Check a datagrid checkbox column is checked not working


Jul 12, 2005
wexford, Ireland
Programming Experience

I have a datagrid with a template checkbox column.

Which I create programatically using the following code
'insert checkbox and load with data
Private Sub DataGridFinanceInvoiceHistory_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGridFinanceInvoiceHistory.ItemDataBound
' process data rows only (skip the header, footer etc.)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
' get a reference to the checkbox in this row
Dim ckbx1 As CheckBox
ckbx1 = CType(e.Item.Cells(10).FindControl("Checkbox1"), CheckBox)
' which cell has been exported
If e.Item.Cells(15).Text = "1" Then
' check it and disable it
' e.Item.Cells(10).Text()
ckbx1.Checked = True
ckbx1.Enabled = False
'ckbx1.Checked = False
ckbx1.Enabled = True
End If
' now add the checkbox
End If
Catch ex As Exception
End Try
End Sub

This is the checkbox column

<asp:TemplateColumn HeaderText="Export">
<ItemStyle HorizontalAlign="Center">
<asp:CheckBox id=CheckBox1 runat="server" AutoPostBack="True"></asp:CheckBox>

So the datagrid items that are already "exported" have their check boxes ticked and the checkboxes are disabled
What I want to do is allow the user to check any number of the remaining checkboxes and then press an export button.
I will use the following code to ignore any checkboxes which are disabled and checked but to read in those which are checked and enabled.

The code works to see if the checkbox is disabled/enabled
But it fails to find the checkboxes which the user has checked though it can see which ones have been checked programatically

Does any one have an idea why this piece of code keeps failing

chkSelected = CType(RowItem.Cells(10).FindControl("CheckBox1"), CheckBox).Checked

It always comes back as false even when the user has ticked the checkbox then pressed the Export button.
When I view it as a watch it is always false
It must be something int he properties but all my enable view states are set to true

Any ideas

Thanks in advance

Private Sub btnExportCheckedItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportCheckedItems.Click
Dim BoolChecked As Boolean = False
Dim RowItem As DataGridItem
Dim chkSelected As Boolean
Dim chkEnabled As Boolean
Dim strAllChecked As String
Dim strAllCheckedEnabled As String
' Go through all the rows in the datagrid
For Each RowItem In DataGridFinanceInvoiceHistory.Items
' If the row is not a header or a footer
If RowItem.ItemType <> ListItemType.Header And RowItem.ItemType <> ListItemType.Footer Then
' Find if the checkbox is checked
chkSelected = CType(RowItem.Cells(10).FindControl("CheckBox1"), CheckBox).Checked
' if any of the checkboxes are newly selected then the sql update can go ahead
If chkSelected = True Then
strAllChecked = strAllChecked & RowItem.Cells(0).Text & ", "
End If
chkEnabled = CType(RowItem.Cells(10).FindControl("CheckBox1"), CheckBox).Enabled
If chkEnabled = True Then
' allow the SQL to go ahead
BoolChecked = True
' store the item primary keys
strAllCheckedEnabled = strAllCheckedEnabled & RowItem.Cells(0).Text & ", "
End If
End If
Top Bottom