web Datagrid checkbox column postback problem

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Norman Katz (nkatz1_at_yahoo.com)
Date: 10/15/04

  • Next message: G Stark: "Dynamic DropDownLists in a DataGrid"
    Date: 15 Oct 2004 15:28:57 -0700
    
    

    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!


  • Next message: G Stark: "Dynamic DropDownLists in a DataGrid"

    Relevant Pages

    • RE: Webpage app Im trying to develop
      ... Suresh. ... On a webpage I want to display a table with rows of data from a database ... However against each row of data I want a column with a checkbox. ... Is there a way to add a checkbox to each row of the datagrid? ...
      (microsoft.public.dotnet.framework.aspnet)
    • How to bind a checkbox in a datagrid to a database field (C# example required)
      ... others show the examples in VB.NET or show it adding a checkbox during ... After adding a datagrid control to a web form, ... eventually bound to a boolean field in the database. ... I retrieve a dataset and bind it to the datagrid. ...
      (microsoft.public.dotnet.framework.aspnet)
    • DataGrid Question
      ... existing in a directory on the server in a DataGrid. ... CheckBox should be "chkJOE*D". ... Dim dInfo As DirectoryInfo ... Dim strListItem As String ...
      (microsoft.public.dotnet.framework.aspnet)
    • RE: Checkbox in datagrid....
      ... Because i am acutally populating my datagrid when some property is ... Try moving the populateDataGrid step to the page_init. ... I have a datagrid which has one of the columns as checkbox. ... private void Page_Load ...
      (microsoft.public.dotnet.framework.aspnet.webcontrols)
    • NEWBIE: DataGrid CheckBox Question
      ... to fill a DataGrid with an ArrayList of Project objects in a Windows ... desired boolean checkbox in the first column. ... select is the boolean that I set to false for each Project object. ...
      (microsoft.public.dotnet.languages.csharp)