Access Individual Row Template in GridView - asp.net

I have a ListBox (named ListBoxProperty) and button (btnDeselectAll) in a TemplateField on a gridview control that shows on every row of the gridview. The code shown below is the code behind the button control in the TemplateField. This code works on every ListBox on every row in the gridview. I would like it to only affect the ListBox in the gridview row from which it was clicked. I've tried a few things with failure so I didn't include my failings - just the portion that almost gives me what I want. Couldn't include a pic alas my reputation is weak.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
For Each Row As GridViewRow In GridView1.Rows
Dim myListBox As ListBox = _
CType(Row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
Next
End Sub

The NamingContainer of the button is the GridViewRow, there you are.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
Dim btn = DirectCast(sender, Button)
Dim row = DirectCast(btn.NamingContainer, GridViewRow)
Dim myListBox = DirectCast(row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
End Sub

Related

GridView SelectedIndexChanged - return item that was clicked

I have an ASP.NET DataGrid that returns three clickable columns at the end. When running SelectedIndexChanged how can I return the value of the item that was clicked?
Thanks
Private Sub DG_SelectedIndexChanged(sender As Object, e As EventArgs)
Try
Dim DG As GridView = CType(sender, GridView)
Dim vID As Integer = DG.SelectedRow.Cells(0).Text
'Need to determine which item was clicked here
Catch ex As Exception
End Try
End Sub
In the end I found the only way was to add an onclick event to the LinkButton
AG_Link.Text = "View Agenda"
AG_Link.Attributes.Add("onclick", "returnDocumentType('Agenda');")
Add some JavaScript to update a hidden textbox, then pick up the result
Private Sub DG_SelectedIndexChanged(sender As Object, e As EventArgs)
Try
Dim DG As GridView = CType(sender, GridView)
Dim vID As Integer = DG.SelectedRow.Cells(0).Text
Dim DocumentType As String = DocumentTypeTB.Text
Catch ex As Exception
End Try
End Sub
Bit of a long way round, but it works :-)

Make reference to a Button in a repeater?

I have a form with a button control in a repeater, and I am trying to reference it in my code so that I can disable and enable it where appropriate but it isn't recognized.
I tried something that looked like this:
Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
'Get the reference of the clicked button.
Dim button As Button = CType(sender, Button)
'Get the command argument
Dim commandArgument As String = button.CommandArgument
'Get the Repeater Item reference
Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)
'Get the repeater item index
Dim index As Integer = item.ItemIndex
item.Enabled = false
End Sub
But item.enabled didn't work like btnPrev.enabled=false would
And this is what I have in my button form control:
asp:Button ID="btnPrev" Text="Previous" OnClick="btnClickPrev" CommandArgument = '1' runat="server"
Any help would be appreciated. thank you
Problem is, RepeaterItem doesn't have any Enable property that you are trying to change. Instead, loop through all controls inside RepeaterItem and disable them like this:
Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
'Get the reference of the clicked button.
Dim button As Button = CType(sender, Button)
'Get the Repeater Item reference
Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)
For Each con In item.Controls
'Disable button
If TypeOf con Is Button Then
CType(con, Button).Enabled = False
End If
'Disable other controls if you want
If TypeOf con Is CheckBox Then
CType(con, CheckBox).Enabled = False
End If
Next
'item.Enabled = False 'Wouldn't work
End Sub

Programmatically update a bound checkbox in a GridView using an asp:ButtonField in ASP.NET 2005

The GridView is bound to an Access Table, but I want to update the checkbox using the asp:ButtonField. The best I've been able to do is replace the checkbox with a string value. Below is the code I have now:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim selectedRow As GridViewRow = GridView1.Rows(index)
'Assign checkbox cell as table cell
Dim selectedCell As TableCell = selectedRow.Cells(0)
'Assign a value to the checkbox, **but instead is replacing the checkbox**
selectedCell.Text = "True"
End If
End Sub
I think you need to use .FindControl().
The GridViewCell is not the same thing as a CheckBox control inside it. You code should probably look more like:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim checkBox As CheckBox = GridView1.Rows(index).FindControl("checkBoxIdGoesHere")
checkBox.Checked = true 'or false or whatever based on your command.
End If
End Sub
I've had my own share of problems accessing controls from within GridViews. I solved something similar about a month ago. Assuming that your checkbox control is the only thing in that cell, you could try something like this:
'Assign a value to the checkbox
CType(GridView1.Rows(index).Cells(0).Controls(0), CheckBox).Checked = True
Try that and see if it works. If not, post back and we can take another look.

whats wrong in this code?

whats wrong in this code
i have a Imagebutton2 in gridview whose commandname is xxx and i have added modalpopup extendar panel2 with ImageButton2 i want when image button 2 will be clicked then the modalpopup will display and retrieve the values from selected gridview row to the literal3 control of panel 1 which acts as a modalpopup control for gridview ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim myrow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "xxx" Then
Dim lab5 As Label = DirectCast(myrow.FindControl("Label5"), Label)
Dim lit3 As Literal = Me.Panel2.FindControl("Literal3")
lit3.Text = lab5.Text
End If
End Sub
Many things can fail:
e.CommandArgument may not be an integer
e.CommandSource may not be of Control type
the NamingContainer may not be of GridViewRow type
myRow can be null, at least when the code is ran
the "Label5" control may not be found, or may not be of Label type, when the code is ran
the "Literal3" control may not be found (null ref) or may not be of Literal type, when the code is ran
...

Referencing items within a gridview

I have a a gridview attached to an objectdatasource. In each row I have a bound textbox for input. I have a button beside the textbox in each row that launches a javascript popup for unit conversion.
The question is: how can i tell the unit converter (js function) what textbox (in which row) to populate the results with?
In the RowCreated event of the GridView:
Protected Sub MyGridView_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles MyGridView.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim btn As Button = e.Row.Cells(0).FindControl("btnJavaScriptButton")
Dim txt As TextBox = e.Row.Cells(1).FindControl("txtResults")
btn.OnClientClick = "calculate(" & txt.ClientID & ");"
End If
End Sub
Where 0 and 1 are the indices of the columns that contain the button and the textbox, and "calculate" is the name of your JavaScript function.

Resources