web Datagrid checkbox column postback problem

Discussion in 'ASP .Net Datagrid Control' started by Norman Katz, Oct 15, 2004.

  1. Norman Katz

    Norman Katz Guest

    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!
    Norman Katz, Oct 15, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TPS

    Checkbox postback in DataGrid

    TPS, Aug 9, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    11,332
  2. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    808
    David Wier
    Oct 6, 2006
  3. Eric

    checkbox column in a datagrid problem

    Eric, Jul 16, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    242
  4. Gregory Rampton

    Convert an MS Access Yes/No column to a checkbox column in C# datagrid

    Gregory Rampton, Aug 6, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    198
    Gregory Rampton
    Aug 6, 2003
  5. Joey Pang
    Replies:
    4
    Views:
    509
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page