check box values in GridView are unexpectedly unchecked after posting

Last post 01-25-2008 2:51 PM by Bruce Frank. 1 replies.

Sort Posts:

  • check box values in GridView are unexpectedly unchecked after posting

    01-23-2008, 4:22 PM
    • Loading...
    • Bruce Frank
    • Joined on 01-22-2008, 6:55 PM
    • Seattle, WA
    • Posts 3

    I have a web form that displays a GridView where the leftmost column in each row contains a check box.  Unlike the other columns in the GridView, the check boxes are not bound to any data source.  The idea is that the check boxes will be initially unchecked.  The user selects a set of rows from the GridView using the check boxes and then clicks a button.  The web application should then iterate through the rows of the GridView and perform processing on the rows for which the check boxes were checked.  Unfortunately, when the code iterates through the check boxes, all check boxes have a status of being unchecked even though they were checked when the form was submitted.

    The GridView object is declared in the main web application class. 

    public partial class Model : System.Web.UI.Page
    {
        GridView GridViewTankType;
      .  .  .

    The user selects values from drop down lists and then clicks a button.  The web application then retrieves the values from the drop down lists and then passes them as parameters to a stored procedure.  The DataSet returned by the stored procedure gets bound to the GridView and determines the data columns and the actual data to display in the GridView.  The last action in the button click routine is to add a cell containing a check box at the beginning of each row:

    protected void ButtonShowModels_Click(object sender, EventArgs e)
    {
        SqlConnection sqlConnection = new SqlConnection(SqlConnectionString);
        sqlConnection.Open();

        GridViewTankType =
    new GridView();
        PlaceHolder1.Controls.Add(GridViewTankType);

        . . .
        // Code to call stored procedure and load data into dataSet variable.
        . . .

        GridViewTankType.DataSource = dataSet;
        GridViewTankType.DataBind();

        GridViewTankType.HeaderRow.Cells.AddAt(0,
    new TableCell());
        foreach (GridViewRow gridViewRow in GridViewTankType.Rows)
        {
            TableCell tableCell = new TableCell();
            CheckBox cb = new CheckBox();
            tableCell.Controls.Add(cb);
            gridViewRow.Cells.AddAt(0, tableCell);
        }
    }

    Once the user has checked the check boxes of interest there is another button for processing the choices.  The Button_Click routine is able to iterate through the rows of the GridView but inexplicably all check boxes have a value of being unchecked.  This is the code:

    protected void ButtonSetModels_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gridViewRow in GridViewTankType.Rows)
        {
           
    CheckBox cb = (CheckBox)(((TableCell)gridViewRow.Cells[0]).Controls[0]);
           
    if (cb != null && cb.Checked)
            {
            . . .

    In the Visual Studio debugger, I can look at ((System.Web.UI.WebControls.TableRow)(GridViewTankType.Rows[0])).Cells.  Each row has a check box in the first column and the remaining columns have the expected data values that were visible in the web form.  However, the cb.Checked value in the code fragment above is always false so none of the selected rows get processed.

    What am I doing wrong?  Any suggestions as to how to get the program working would be appreciated.  Thanks.

    Filed under: ,
  • Re: check box values in GridView are unexpectedly unchecked after posting

    01-25-2008, 2:51 PM
    Answer
    • Loading...
    • Bruce Frank
    • Joined on 01-22-2008, 6:55 PM
    • Seattle, WA
    • Posts 3

    Well, it turns out that including a check box in each row of the GridView the way I was trying to do it won't work.  As explained in Checking All CheckBoxes in a GridView (http://aspnet.4guysfromrolla.com/articles/052406-1.aspx) by Scott Mitchell and Extending the GridView Control (http://msdn.microsoft.com/msdnmag/issues/06/05/CuttingEdge/) by Dino Esposito, the CheckBoxes need to be bound to the same data source as the GridView or the CheckBoxes needs to be part of a TemplateField column.

    I tried the TemplateField approach but I couldn't get it to work.  The check boxes were inexplicably dropped from the GridView after the form was submitted.  There is probably some simple mistake that I was making. 

    Next I tried using the sample code from the Esposito article which derives a new GridView control and adds the necessary infrastructure to get the checkboxes working.  It adds extra functionality that I don't need but it did work.  I could iterate through the rows of the GridView and identify those which had been checked.

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter