title: Check uncheck all CheckBoxes on the basis of another CheckBox
body: <p>I want to select and unselect all checkboxes when user clicks on the first checkbox <strong>(All)</strong>. And if user unchecks any checkbox, then only that checkbox and the first checkbox <strong>(All)</strong> should be unchecked and no change to the remaining checkboxes.</p>
  
  <p>I have a <code>Checkboxlist</code> in my page which I'm populating dynamically.</p>
  
  <pre><code>&lt;asp:CheckBoxList runat="server" ID="cbExtList" /&gt;
  </code></pre>
  
  <h2>Code Behind</h2>
  
  <pre><code>private Extensions _extension = new Extensions();
  private ExtCollection _extCollection = new ExtCollection();
  
  _extCollection = _extension.GetAll();
  
  Dictionary&lt;int, string&gt; dExtensions = new Dictionary&lt;int, string&gt;();
  
  dExtensions.Add(0, "All");
  foreach (Extensions ext in _extCollection)
  {
      dExtensions.Add(ext.ID, ext.Extension);
  }
  
  this.cbExtList.DataSource = dExtensions;
  this.cbExtList.DataTextField = "Value";
  this.cbExtList.DataValueField = "Key";
  this.cbExtList.DataBind();
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/uhUmj.png" alt="enter image description here"></p>
  
  <p>Now everything is working fine. I just want to select all Extensions when I click on the first checkbox "All" in this <code>Checkboxlist</code>.</p>
  
  <p><strong>This is what I tried with code behind approach.</strong></p>
  
  <p>With the <code>OnSelectedIndexChanged</code> and setting the <code>AutoPostBack = True</code></p>
  
  <pre><code>&lt;asp:CheckBoxList runat="server" ID="cbExtList" OnSelectedIndexChanged="cbExtList_OnSelectedIndexChanged" AutoPostBack="True" /&gt;
  
  protected void cbExtList_OnSelectedIndexChanged(object sender, EventArgs e)
  {
      try
      {
          if (Convert.ToInt32(this.cbExtList.SelectedItem.Value) == 0)
          {
              foreach (ListItem li in cbExtList.Items)
              {
                  li.Selected = true;
              }
          }
          else
          {
              foreach (ListItem li in cbExtList.Items)
              {
                  li.Selected = false;
              }
          }
      }
      catch (Exception ex)
      {
          Monitoring.WriteException(ex);
      }
  }
  </code></pre>