Unable to detect Checked property of Checkbox in Datagrid

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?
 
K

Ken Cox [Microsoft MVP]

Hi Jon,

It is running late and I have to run but here's what I came up with that may
solve your problem. Let us know if it helps?

Ken
MVP [ASP.NET]


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
Dim intCounter1 As Integer
Dim intCounter2 As Integer
If Not IsPostBack Then
sb.Append("<?xml version='1.0' standalone='yes'?>")
sb.Append("<NewDataSet>")
For intCounter1 = 0 To 20
sb.Append("<Table>")
sb.Append("<pro_cde>ITEM" & intCounter1.ToString & "</pro_cde>")
sb.Append("<selected>true</selected>")
sb.Append("</Table>")
Next
sb.Append("</NewDataSet>")
Dim ds As DataSet = New DataSet
Dim xmlSR As System.IO.StringReader = _
New System.IO.StringReader(sb.ToString)
ds.ReadXml(xmlSR, XmlReadMode.InferSchema)
Datagrid1.DataSource = ds
Datagrid1.DataBind()
End If

End Sub
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 chkBx As CheckBox
Dim lbllSKU As Label

For Each dgItem In Datagrid1.Items
chkBx = dgItem.FindControl("chkSelection")
If chkBx.Checked = True Then
lbllSKU = dgItem.FindControl("lblSKU")
prodAry.Add(lbllSKU.Text)
' Response.Write(lbllSKU.Text & "<br>")
End If
Next
Response.Write("Total Items:" & prodAry.Count.ToString & "<br>")
For selCount = 0 To prodAry.Count - 1
Response.Write("Selected:" & prodAry(selCount) & "<br>")
Next
End Sub


--

Important: Patch Windows *beyond* Blaster.
http://www.microsoft.com/technet/security/bulletin/MS03-039.asp


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?
 
J

Jon

Ken,

Thanks for your help... it looks like you tested this yourself.
Unfortunately, it still doesn't work at my end. The array size is
zero.

I'm running VS 2002. Is there any chance that I'm encountering a bug
in the old version of the .NET Framework? The reason I ask is because
I tried using several samples found in newsgroups, and none worked on
my box. It's frustrating to see such as simple algorithm fail. In
any event, I'm going to try writing a work around (maintain a session
array that changes as checkboxes are selected and deselected). If you
have any other ideas, I'd appreciate it.

Jon

Ken Cox said:
Hi Jon,

It is running late and I have to run but here's what I came up with that may
solve your problem. Let us know if it helps?

Ken
MVP [ASP.NET]


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
Dim intCounter1 As Integer
Dim intCounter2 As Integer
If Not IsPostBack Then
sb.Append("<?xml version='1.0' standalone='yes'?>")
sb.Append("<NewDataSet>")
For intCounter1 = 0 To 20
sb.Append("<Table>")
sb.Append("<pro_cde>ITEM" & intCounter1.ToString & "</pro_cde>")
sb.Append("<selected>true</selected>")
sb.Append("</Table>")
Next
sb.Append("</NewDataSet>")
Dim ds As DataSet = New DataSet
Dim xmlSR As System.IO.StringReader = _
New System.IO.StringReader(sb.ToString)
ds.ReadXml(xmlSR, XmlReadMode.InferSchema)
Datagrid1.DataSource = ds
Datagrid1.DataBind()
End If

End Sub
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 chkBx As CheckBox
Dim lbllSKU As Label

For Each dgItem In Datagrid1.Items
chkBx = dgItem.FindControl("chkSelection")
If chkBx.Checked = True Then
lbllSKU = dgItem.FindControl("lblSKU")
prodAry.Add(lbllSKU.Text)
' Response.Write(lbllSKU.Text & "<br>")
End If
Next
Response.Write("Total Items:" & prodAry.Count.ToString & "<br>")
For selCount = 0 To prodAry.Count - 1
Response.Write("Selected:" & prodAry(selCount) & "<br>")
Next
End Sub


--

Important: Patch Windows *beyond* Blaster.
http://www.microsoft.com/technet/security/bulletin/MS03-039.asp


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?
 
K

Ken Cox [Microsoft MVP]

There shouldn't be any difference at all. Did you remember to include this:

Dim prodAry As New ArrayList

I've inserted the complete code below in hopes it was just something small.

Ken

Public Class chkclick
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Datagrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents btnAdd As System.Web.UI.WebControls.Button
Dim prodAry As New ArrayList

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sb As New System.Text.StringBuilder
Dim intCounter1 As Integer
Dim intCounter2 As Integer
If Not IsPostBack Then
sb.Append("<?xml version='1.0' standalone='yes'?>")
sb.Append("<NewDataSet>")
For intCounter1 = 0 To 20
sb.Append("<Table>")
sb.Append("<pro_cde>ITEM" & intCounter1.ToString & "</pro_cde>")
sb.Append("<selected>true</selected>")
sb.Append("</Table>")
Next
sb.Append("</NewDataSet>")
Dim ds As DataSet = New DataSet
Dim xmlSR As System.IO.StringReader = _
New System.IO.StringReader(sb.ToString)
ds.ReadXml(xmlSR, XmlReadMode.InferSchema)
Datagrid1.DataSource = ds
Datagrid1.DataBind()
End If

End Sub
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 chkBx As CheckBox
Dim lbllSKU As Label

For Each dgItem In Datagrid1.Items
chkBx = dgItem.FindControl("chkSelection")
If chkBx.Checked = True Then
lbllSKU = dgItem.FindControl("lblSKU")
prodAry.Add(lbllSKU.Text)
' Response.Write(lbllSKU.Text & "<br>")
End If
Next
Response.Write("Total Items:" & prodAry.Count.ToString & "<br>")
For selCount = 0 To prodAry.Count - 1
Response.Write("Selected:" & prodAry(selCount) & "<br>")
Next
End Sub

End Class


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="chkclick.aspx.vb"
Inherits="p733workev.chkclick"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>chkclick</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="FlowLayout">
<form id="Form1" method="post" runat="server">
&nbsp;
<asp:datagrid id="Datagrid1" runat="server" width="555px"
cssclass="dg_full" autogeneratecolumns="False"
horizontalalign="Center" datakeyfield="pro_cde">
<columns>
<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:templatecolumn headertext="Add">
<headerstyle cssclass="colhead_full"></headerstyle>
<itemstyle horizontalalign="Center"
cssclass="normalcell_full"></itemstyle>
<itemtemplate>
<asp:checkbox id="chkSelection" runat="server"
checked='<%#Databinder.Eval(Container.DataItem, "selected") %>' />
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
<asp:button id="btnAdd" runat="server" text="Button"></asp:button>
</form>
</body>
</html>
 
J

Jon

Ken,

Thanks for your help. I finally got it to work by starting with a new
ASPX file and copying and pasting the code. No idea why the original
didn't work.

Jon
 
K

Ken Cox [Microsoft MVP]

Glad to hear you got going.

Sometimes I think VS.NET has a mind of its own when dealing with code updates.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top