J
Jon
I'd appreciate any help in troubleshoothing this code- it's probably
simple but I'm lost at the moment.
A form displays a list of items in a datagrid. Each row includes a
bound checkbox to let the user select one or more rows. When the form
is submitted, I want to simply determine which rows contain selected
checkboxes.
Here's the datagrid. The checkbox field is bound to a 'bit' datatype
field:
<asp:datagrid id="dgSKU" runat="server" DataKeyField="pro_cde"
HorizontalAlign="Center" AutoGenerateColumns="False"
CssClass="dg_full" Width="555px">
<Columns>
<asp:BoundColumn DataField="pro_des" ReadOnly="True"
HeaderText="Description">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="SKU">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblSKU" Text='<%#
DataBinder.Eval(Container.DataItem,"pro_cde") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="history" ReadOnly="True"
HeaderText="History Wks">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Add"
ItemStyle-HorizontalAlign="Center">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkSelection" Runat="server" Checked='<%#
Databinder.Eval(Container.DataItem, "selected") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
And here's the code. When I run the debugger, I find that the second
IF statement never returns TRUE (If chkSelected.Checked Then). So the
selCount var never gets incremented and always equals 0, no matter how
many checkboxes in the datagrid are selected:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim selCount As Integer
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Dim prodSKU As String
Dim prodAry As New ArrayList()
Dim prod_code As String
selCount = 0
For Each dgItem In dgSKU.Items
chkSelected = CType(dgItem.FindControl("chkSelection"),
CheckBox)
If Not chkSelected Is Nothing Then
If chkSelected.Checked Then
prodSKU = CType(dgItem.FindControl("lblSKU"),
Label).Text
prodAry.Add(CStr(prodSKU))
selCount = selCount + 1
End If
End If
Next
End Sub
Any ideas?
simple but I'm lost at the moment.
A form displays a list of items in a datagrid. Each row includes a
bound checkbox to let the user select one or more rows. When the form
is submitted, I want to simply determine which rows contain selected
checkboxes.
Here's the datagrid. The checkbox field is bound to a 'bit' datatype
field:
<asp:datagrid id="dgSKU" runat="server" DataKeyField="pro_cde"
HorizontalAlign="Center" AutoGenerateColumns="False"
CssClass="dg_full" Width="555px">
<Columns>
<asp:BoundColumn DataField="pro_des" ReadOnly="True"
HeaderText="Description">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="SKU">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
<ItemTemplate>
<asp:Label ID="lblSKU" Text='<%#
DataBinder.Eval(Container.DataItem,"pro_cde") %>' runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="history" ReadOnly="True"
HeaderText="History Wks">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Add"
ItemStyle-HorizontalAlign="Center">
<HeaderStyle CssClass="colhead_full"></HeaderStyle>
<ItemStyle CssClass="normalcell_full"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkSelection" Runat="server" Checked='<%#
Databinder.Eval(Container.DataItem, "selected") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
And here's the code. When I run the debugger, I find that the second
IF statement never returns TRUE (If chkSelected.Checked Then). So the
selCount var never gets incremented and always equals 0, no matter how
many checkboxes in the datagrid are selected:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Dim selCount As Integer
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Dim prodSKU As String
Dim prodAry As New ArrayList()
Dim prod_code As String
selCount = 0
For Each dgItem In dgSKU.Items
chkSelected = CType(dgItem.FindControl("chkSelection"),
CheckBox)
If Not chkSelected Is Nothing Then
If chkSelected.Checked Then
prodSKU = CType(dgItem.FindControl("lblSKU"),
Label).Text
prodAry.Add(CStr(prodSKU))
selCount = selCount + 1
End If
End If
Next
End Sub
Any ideas?