web Datagrid checkbox column postback problem

N

Norman Katz

Has anyone created a database-driven web DataGrid with an extra
checkbox column that is not bound to the grid, but must be read back
when the post occurs? This used to be so easy in ASP with the
Request.Form collection but it's not clear from the MS Docs how to
insure that the checkbox's checked state gets bound back to the
DataGrid or some other array or collection.

I have a web DataGrid that has one template column containing a
checkbox and a set of columns that are bound to a database query via a
DataReader as follows:

Dim dr As SqlClient.SqlDataReader
SqlConnection1.Open()
dr = cmdGetRows.ExecuteReader()
DataGrid1.DataSource = dr
DataGrid1.DataBind()

I created the template column in the designer and added the checkbox,
as explained in KB article Q306227. I can bind to the checkbox before
the page is sent to to the client with either a column of my
dataReader or a separate variable bound at runtime in a
DataGrid1_ItemDataBound() handler. But whenever the page is posted
back to the server, the checkbox state (on every row) is set to false.
So the binding is somehow not occurring on the postback.

The template looks like this:

<asp:TemplateColumn HeaderText="Print">
<ItemTemplate>
<asp:CheckBox id=cbPrint runat="server">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>

I've tried including the attribute: Checked='<%#
DataBinder.Eval(Container, "DataItem.cb") %>'>
Where cb is a column name of the database query. This works fine on
the outgoing side as well.
I've also tried simply Checked=true/false.

The rendered checkboxes always get displayed correctly on the browser
but the postback doesn't assign the cb state back to the datagrid
cells.

I use a WebControls.LinkButton for the postback and it definitely pops
right into the _Click event handler for this button. I then iterate
through the DataGrid using several methods, namely, looking at each
row or DataGridItem in a loop as follows:

Dim cb As CheckBox
Dim dgi As DataGridItem
Dim idList As String = ""

'checkboxes are always the 2nd item (index 1) of their
respective cell control list
'DataGrid.Items is a list of the actual Row objects
For Each dgi In DataGrid1.Items
cb = dgi.FindControl("cbPrint")
If cb.Checked Then
‘do something here
End If
Next

Another way was:
For Each dgi In DataGrid1.Items
cb = CType(dgi.Cells(CB_COLUMN).Controls(1), CheckBox)
If cb.Checked Then
‘do something here
End If
Next


The looping and finding the checkboxes was no problem. But whenever
it enters this handler, the Checkboxes are never in the Checked state.
I actually saw them in the checked state early on in my debugging but
found that when I ran with no breakpoints, it never got into the "do
something" code. So it seems like the binding on the way back to the
server is not occurring correctly.


Thanks!
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top