jschnei555
asked on
Read checkbox state in ASP.Net datagrid
This datagrid on my aspx page...
<asp:DataGrid id="dgLineItems" runat="server" AutoGenerateColumns="False ">
<Columns>
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox id="cbSelect" runat="server"></asp:Check Box>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="LNITMSEQ" HeaderText="Line ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ITEMNMBR" HeaderText="Item ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ITEMDESC" HeaderText="Item Description"></asp:BoundCo lumn>
<asp:BoundColumn DataField="UOFM" HeaderText="UOM"></asp:Bou ndColumn>
<asp:BoundColumn DataField="QUANTITY" HeaderText="Qty" DataFormatString="{0:N0}">
<ItemStyle HorizontalAlign="Right"></ ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="UNITPRCE" HeaderText="Price" DataFormatString="{0:N2}">
<ItemStyle HorizontalAlign="Right"></ ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="XTNDPRCE" HeaderText="Ext Price" DataFormatString="{0:N2}">
<ItemStyle HorizontalAlign="Right"></ ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid></P>
This code for a button on the aspx page...
Private Sub buttonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonNext.Click
Dim dgItem As DataGridItem
Dim i As Integer = 0
For Each dgItem In dgLineItems.Items
Dim myCheckbox As CheckBox = CType(dgItem.Cells(0).Cont rols(1), CheckBox)
If myCheckbox.Checked = True Then
i += 1
End If
Next
doSomething(i)
End Sub
... "i" is always zero even if I check some of the checkboxes. Any suggestions? Thanks.
<asp:DataGrid id="dgLineItems" runat="server" AutoGenerateColumns="False
<Columns>
<asp:TemplateColumn HeaderText="Select">
<ItemTemplate>
<asp:CheckBox id="cbSelect" runat="server"></asp:Check
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="LNITMSEQ" HeaderText="Line ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ITEMNMBR" HeaderText="Item ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ITEMDESC" HeaderText="Item Description"></asp:BoundCo
<asp:BoundColumn DataField="UOFM" HeaderText="UOM"></asp:Bou
<asp:BoundColumn DataField="QUANTITY" HeaderText="Qty" DataFormatString="{0:N0}">
<ItemStyle HorizontalAlign="Right"></
</asp:BoundColumn>
<asp:BoundColumn DataField="UNITPRCE" HeaderText="Price" DataFormatString="{0:N2}">
<ItemStyle HorizontalAlign="Right"></
</asp:BoundColumn>
<asp:BoundColumn DataField="XTNDPRCE" HeaderText="Ext Price" DataFormatString="{0:N2}">
<ItemStyle HorizontalAlign="Right"></
</asp:BoundColumn>
</Columns>
</asp:DataGrid></P>
This code for a button on the aspx page...
Private Sub buttonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonNext.Click
Dim dgItem As DataGridItem
Dim i As Integer = 0
For Each dgItem In dgLineItems.Items
Dim myCheckbox As CheckBox = CType(dgItem.Cells(0).Cont
If myCheckbox.Checked = True Then
i += 1
End If
Next
doSomething(i)
End Sub
... "i" is always zero even if I check some of the checkboxes. Any suggestions? Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try like this, try using FindControl method.
Private Sub buttonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonNext.Click
Dim dgItem As DataGridItem
Dim i As Integer = 0
For Each dgItem In dgLineItems.Items
Dim myCheckbox As CheckBox = CType(dgItem.FindControl(" cbSelect") , CheckBox)
If myCheckbox.Checked = True Then
i += 1
End If
Next
doSomething(i)
End Sub
Private Sub buttonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonNext.Click
Dim dgItem As DataGridItem
Dim i As Integer = 0
For Each dgItem In dgLineItems.Items
Dim myCheckbox As CheckBox = CType(dgItem.FindControl("
If myCheckbox.Checked = True Then
i += 1
End If
Next
doSomething(i)
End Sub
ASKER
appari, I already gave the points to laozti2000 for answering the question, but I do appreciate your comment. That's a nice trick.
ASKER