Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > web Datagrid checkbox column postback problem

Thread Tools

web Datagrid checkbox column postback problem

Norman Katz
Posts: n/a
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
dr = cmdGetRows.ExecuteReader()
DataGrid1.DataSource = dr

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">
<asp:CheckBox id=cbPrint runat="server">

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

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

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

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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
Having a checkbox column and the grid disabled upon first load generated wierd behavior when Postback occurs. glorfindel ASP .Net Datagrid Control 1 02-19-2005 09:44 AM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
Convert an MS Access Yes/No column to a checkbox column in C# datagrid Gregory Rampton ASP .Net Datagrid Control 0 08-06-2003 04:09 PM
checkbox column in a datagrid problem Eric ASP .Net Datagrid Control 2 07-16-2003 08:25 PM