title: OnCheckChanged Event of asp:CheckBox Not Firing When Unchecked From Within Nested GridView
body: <p>I have conducted a lot of searches which included checkchanged or OncheckChanged keyword:</p>
  
  <p><a href="https://stackoverflow.com/questions/3457136/asp-net-checkbox-does-not-fire-checkedchanged-event-when-unchecking">ASP.NET CheckBox does not fire CheckedChanged event when unchecking</a></p>
  
  <p><a href="https://forums.asp.net/t/1311576.aspx?Checkbox+not+firing+when+unchecking+using+OnCheckedChanged" rel="nofollow noreferrer">https://forums.asp.net/t/1311576.aspx?Checkbox+not+firing+when+unchecking+using+OnCheckedChanged</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6596767/oncheckedchanged-event-not-firing">OnCheckedChanged event not firing</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1404528/oncheckedchanged-event-handler-of-aspcheckbox-does-not-fire-when-checkbox-is-un?rq=1">OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked</a></p>
  
  <p>But this just doesn't seem to be working although I applied all suggestion from the links given above</p>
  
  <p>I am tring to fire a <strong>CheckChanged Event</strong> From nested gridview, whose <code>DataSource</code> gets binded in parent grid's <code>OnRowDataBound</code> event.</p>
  
  <p>My <strong>aspx markup</strong></p>
  
  <pre><code>&lt;asp:GridView ID="gvDocSchedule" runat="server" AutoGenerateColumns="false" CssClass="table table-striped color-black"
      OnRowDataBound="gvDocSchedule_RowDataBound" DataKeyNames="UserID"&gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
                  &lt;img alt="" style="cursor:pointer" src="UserPanel/images/Plus12.png" /&gt;                
                  &lt;asp:Panel ID="PanelSchedule" runat="server" Style="display:none"&gt;
                      &lt;asp:GridView ID="gvScheduleDayNTime" runat="server" AutoGenerateColumns="false"&gt;
                          &lt;Columns&gt;
                              &lt;asp:BoundField ItemStyle-Width="150px" DataField ="ScheduleDay" HeaderText="CheckInTime1"/&gt;
                              &lt;asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime1" HeaderText="CheckInTime1"/&gt;
                              &lt;asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime1" HeaderText="CheckOutTime1"/&gt;
                              &lt;asp:BoundField ItemStyle-Width="150px" DataField ="CheckInTime2" HeaderText="CheckInTime2"/&gt;
                              &lt;asp:BoundField ItemStyle-Width="150px" DataField ="CheckOutTime2" HeaderText="CheckOutTime2"/&gt;
                              &lt;asp:TemplateField&gt;
                                  &lt;ItemTemplate&gt;
                                      &lt;asp:HiddenField ID="hdnForChkBox" runat="server" value="No" /&gt; 
  
                                      &lt;asp:CheckBox ID="CBoxAvailabilityTime1" ViewStateMode="Enabled" Checked="false" Enabled="true" EnableViewState="true" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true"  OnCheckedChanged="CBoxAvailabilityTime1_CheckedChanged" /&gt;
                                      &lt;br /&gt;
                                      &lt;asp:CheckBox ID="CBoxAvailabilityTime2" runat="server" Text="See Free TimeSlots For Booking In Time1" AutoPostBack="true" OnCheckedChanged="CBoxAvailabilityTime2_CheckedChanged" /&gt;
                                  &lt;/ItemTemplate&gt;
                              &lt;/asp:TemplateField&gt;
                          &lt;/Columns&gt;
                      &lt;/asp:GridView&gt;
                  &lt;/asp:Panel&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField ControlStyle-Font-Bold="true" DataField="UserFirstName" HeaderText="Doctor's First Name" /&gt;
          &lt;asp:BoundField DataField="UserLastName" HeaderText="Doctor's Last Name" /&gt;
          &lt;asp:BoundField DataField="SpecializationName" HeaderText="Doctor's Specialization" /&gt;
          &lt;asp:BoundField DataField="HospitalName" HeaderText="Hospital" /&gt;
  
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>I was trying it with <code>CBoxAvailabilityTime1</code>CheckBox. As you guys can see ViewStateMode and EnableViewState has been taken care of not only in server tag but also in content page's tag, also these properties are true for master page</p>
  
  <p>Parent Grid gets binded when user press a button and nested grid gets binded in parent's <code>OnRowDataBound</code> event</p>
  
  <p>Here is My <strong>aspx.cs code</strong></p>
  
  <pre><code>protected void CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)
  {
      foreach (GridViewRow gvParentRow in gvDocSchedule.Rows)
          {
              if (gvParentRow.RowType == DataControlRowType.DataRow)
              {
                  GridView gvDocSchedulesGetChildGrid = (GridView)gvParentRow.FindControl("gvScheduleDayNTime");
                  if (gvDocSchedulesGetChildGrid != null)
                  {
                      foreach (GridViewRow gvChildRow in gvDocSchedulesGetChildGrid.Rows)
                      {
                          CheckBox CBoxAvailabilityTime1 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime1");
                          CheckBox CBoxAvailabilityTime2 = (CheckBox)gvChildRow.FindControl("CBoxAvailabilityTime2");
  
                          if (((CheckBox)CBoxAvailabilityTime1).Checked)
                          {
                              CBoxAvailabilityTime2.Enabled = false;
                          }
                          if (!((CheckBox)CBoxAvailabilityTime1).Checked)
                          {
                              CBoxAvailabilityTime2.Enabled = true;
                          }
                     }
                }
            }
       }
  }
  </code></pre>
  
  <p>With this setup CheckChanged Event Fires on Checking.
  On Checking it hits <code>Page_ Load</code> <strong>skips</strong> <code>if(!IsPostBack)</code> (as <code>AutoPostBack=true</code>) and then control is directly transferred to
  <code>CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)</code> <strong>event handler function</strong>
  but on the other hand it doesn't fire on unchecking it <code>postsbacks</code> goes to <code>Page load</code> again <strong>Skips</strong> <code>if(!IsPostBack)</code> and does nothing instead of calling
  <code>CBoxAvailabilityTime1_CheckedChanged(object sender, EventArgs e)</code></p>
  
  <p><strong>"Note:-"</strong> <code>Page_Load</code> is not involved. I can't bind parent grid in page_load,
  <code>!IsPostBack</code> because I dont need to bind it at first time when page gets loaded rather its binding happens inside a button click event, if a button is clicked only then parent grid must get binded.   </p>
  
  <p><strong>Update 1:-</strong> <strong>This is how I bind data to parent grid</strong></p>
  
  <p>I am calling this function in a button click event   </p>
  
  <pre><code>protected void BindDataToGridViewDocInArea()
      {
          using (SqlConnection con = new SqlConnection(constr))
          {
              con.Open();
              SqlCommand cmdFillGridDocInArea = new SqlCommand("some query with parameter here", con)
              cmdFillGridDocInArea.Parameters.AddWithValue("@AID", ddlAskArea.SelectedValue);
              SqlDataAdapter sdaFillGridDocInArea = new SqlDataAdapter(cmdFillGridDocInArea);
              DataTable dtFillGridDocInArea = new DataTable();
              sdaFillGridDocInArea.Fill(dtFillGridDocInArea);
              if (dtFillGridDocInArea.Rows.Count &gt; 0)
              {
                  gvDocSchedule.DataSource = dtFillGridDocInArea;
                  gvDocSchedule.DataBind();
              }
              else
              {
                  lblError.Text = string.Empty;
                  lblError.Text = "No Record Exists Against Requst Specified";
              }
              con.Dispose();
           }
      }
  </code></pre>