jschnei555
asked on
AddHandler not catching event for CheckBox in GridView
I have this gridview with a checkbox whose checked event I'm trying to capture :
<asp:GridView ID="RMAGridView" runat="server" AutoGenerateColumns="False " DataKeyNames="UID" >
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" EnableViewState="true" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's where I add the handler:
Protected Sub RMAGridView_RowDataBound(B yVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles RMAGridView.RowDataBound
...
If e.Row.RowType = DataControlRowType.DataRow Then
...
Dim cb As CheckBox = CType(e.Row.Cells(12).Cont rols(1), CheckBox)
AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged
End If
End Sub
Public Sub cb_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
End Sub
The problem is that when I check one of those checkboxes, a postback does take place but cb_CheckChanged is never called. Am I missing something here? I've seen several examples, including this one: http://www.vbcity.com/forums/faq.asp?fid=37&cat=Web+Development&#TID117940 and just don't see what I'm doing wrong. I know this is the weekend, but I'm hoping for a pretty quick response.
<asp:GridView ID="RMAGridView" runat="server" AutoGenerateColumns="False
<Columns>
...
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" EnableViewState="true" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here's where I add the handler:
Protected Sub RMAGridView_RowDataBound(B
...
If e.Row.RowType = DataControlRowType.DataRow
...
Dim cb As CheckBox = CType(e.Row.Cells(12).Cont
AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged
End If
End Sub
Public Sub cb_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
End Sub
The problem is that when I check one of those checkboxes, a postback does take place but cb_CheckChanged is never called. Am I missing something here? I've seen several examples, including this one: http://www.vbcity.com/forums/faq.asp?fid=37&cat=Web+Development&#TID117940 and just don't see what I'm doing wrong. I know this is the weekend, but I'm hoping for a pretty quick response.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Protected Sub RMAGridView_RowCreated(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles RMAGridView.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cb As CheckBox = CType(e.Row.Cells(12).Cont rols(1), CheckBox)
AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged
End If
End Sub
That did it. Thanks for the quick response.
If e.Row.RowType = DataControlRowType.DataRow
Dim cb As CheckBox = CType(e.Row.Cells(12).Cont
AddHandler cb.CheckedChanged, AddressOf cb_CheckedChanged
End If
End Sub
That did it. Thanks for the quick response.
AddHandler cb.Click, AddressOf Me.cb_CheckedChanged