title: GridView CheckBox If checked is defaulted but users unchecks, on postback checkbox is still checked
body: <p>I have a page with 5 grids.  The first field in each grid is a  within a template.  Upon pageload and if not postback i set this checked box .checked based on return from database.  </p>
  
  <p>If the user unchecks the checkbox, this checkeboxed.checked = false is not being preserved on postback.  Interesting enough, if the user checks an unchecked box, that change does make it over on post back.  Please let me know if anyone has any suggestions on how to fix. Thank you!</p>
  
  <pre><code>&lt;asp:GridView ID="commonAdultClinicReferrals" CssClass="Report" runat="server" AutoGenerateColumns="false"
                              Width="270" AllowPaging="false" AlternatingRowStyle-CssClass="DataAlternateRow"
                              CellPadding="1" CellSpacing="1" HeaderStyle-HorizontalAlign="Center" EmptyDataText="No Results."
                              DataKeyNames="Id,Name,Description,FollowUpList"  OnRowDataBound="GridView_RowDataBound"&gt;
                              &lt;Columns&gt;
                                  &lt;asp:TemplateField ItemStyle-Width="10"&gt;
                                      &lt;ItemTemplate&gt;
                                          &lt;center&gt;
                                              &lt;asp:CheckBox ID="selectedRowCb" runat="server"  /&gt;
                                          &lt;/center&gt;
                                      &lt;/ItemTemplate&gt;
                                  &lt;/asp:TemplateField&gt;
                                  &lt;asp:BoundField DataField="Name" HeaderText="Common Adult Clinic Referrals" ItemStyle-Width="250"
                                      ItemStyle-Wrap="true" /&gt;
                              &lt;/Columns&gt;
                          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Class:</p>
  
  <pre><code>public class FollowUp
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public bool FollowUpList { get; set; }
      public bool Selected { get; set; }
  
      public FollowUp(int id, string name, string description, bool followUpList, bool selected)
      {
          this.Id = id;
          this.Name = name;
          this.Description = description;
          this.FollowUpList = followUpList;
          this.Selected = selected;
      }
  }
  </code></pre>
  
  <p>DataBind:</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
  {
  
      if (!Page.IsPostBack)
      {
          hdnWindowClose.Value = string.Empty;
  
          loadUi();
      }
  
  
  }
  
  private void loadUi()
  {
  
      _adultServices = GetFollowUps(2, true);
      adultClinicReferrals.DataSource = _adultServices;
      adultClinicReferrals.DataBind();
  </code></pre>
  
  <p>}</p>
  
  <p>Setting Checkbox checked:</p>
  
  <pre><code>protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
  
      if (e.Row.RowType == DataControlRowType.DataRow &amp;&amp; !Page.IsPostBack)
      {
          CheckBox cb = ((CheckBox)e.Row.FindControl("selectedRowCb"));
          FollowUp fol = (FollowUp)e.Row.DataItem;
          bool selected = fol.Selected;
  
          cb.Checked = selected;
  
      }
  
  }
  </code></pre>
  
  <p>On Save:</p>
  
  <pre><code> protected void saveButton_Click(object sender, EventArgs e)
  {
      GetSelectedCodes(followUpAppointments);
      GetSelectedCodes(adultClinicReferrals);
      GetSelectedCodes(commonAdultClinicReferrals);
      GetSelectedCodes(otherAdultClinicReferrals);
      GetSelectedCodes(pediClinicReferrals);
  
      hdnWindowClose.Value = "yes";
  
  
      JavaScriptSerializer js = new JavaScriptSerializer();
      string str = js.Serialize(_selectedServices);
  
      hdnFollowUp.Value = str;
      SetNotePickCodes();
  }
  </code></pre>
  
  <p>At last inside GetSelectedCodes()   Is where checkbox.check state does not work if that check box was defaulted to true:</p>
  
  <pre><code>private void GetSelectedCodes(GridView gv)
  {
  
      for (int i = 0; i &lt; gv.Rows.Count; i++)
      {
          GridViewRow row = gv.Rows[i];
          bool isChecked = ((CheckBox)row.FindControl("selectedRowCb")).Checked;
  
          if (isChecked)
          {
              int id = (Int32)gv.DataKeys[i]["Id"];
              string name = gv.DataKeys[i]["Name"].ToString();
              string description = gv.DataKeys[i]["Description"].ToString();
              bool followup = (bool)gv.DataKeys[i]["FollowUpList"];
  
              _selectedServices.Add(new FollowUp(id,name,description,followup,true));
          }
  
  
      }
  </code></pre>
  
  <p>the if (isChecked) branch is getting executed, when I do not expect it to. For example when the user had unchecked a checkbox.  My databind is done only on page load if not a page.postback.  </p>
  
  <p>Again many thanks for any help.</p>
  
  <p>Thank you,</p>
  
  <p>Jay</p>