Link to home
Start Free TrialLog in
Avatar of jschnei555
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:CheckBox>
</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:BoundColumn>
<asp:BoundColumn DataField="UOFM" HeaderText="UOM"></asp:BoundColumn>
<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).Controls(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.
ASKER CERTIFIED SOLUTION
Avatar of laotzi2000
laotzi2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jschnei555
jschnei555

ASKER

Thanks a lot!
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
appari, I already gave the points to laozti2000 for answering the question, but I do appreciate your comment.  That's a nice trick.