title: Select all CheckBoxes in CheckBoxList
body: <p>I have a <code>CheckBox</code> and a <code>CheckBox</code> list on my web page.<br/>
  If the <code>CheckBox</code> is selected, all the <code>CheckBoxes</code> in the <code>CheckBoxList</code> should get selected, and if the <code>CheckBox</code> is unchecked, similarly all the <code>CheckBoxes</code> in the <code>CheckBox</code> should get deselected (unchecked).</p>
  
  <p><strong>.aspx code</strong></p>
  
  <pre><code>&lt;asp:CheckBoxList ID="CheckBoxList1" runat="server" 
              RepeatDirection="Horizontal" RepeatLayout="Flow"&gt;
              &lt;asp:ListItem&gt;Item A&lt;/asp:ListItem&gt;
              &lt;asp:ListItem&gt;Item B&lt;/asp:ListItem&gt;
              &lt;asp:ListItem&gt;Item C&lt;/asp:ListItem&gt;
              &lt;asp:ListItem Selected="True"&gt;Item D&lt;/asp:ListItem&gt;
              &lt;asp:ListItem&gt;Item E&lt;/asp:ListItem&gt;
              &lt;asp:ListItem&gt;Item F&lt;/asp:ListItem&gt;
              &lt;asp:ListItem&gt;Item G&lt;/asp:ListItem&gt;
          &lt;/asp:CheckBoxList&gt;
  &lt;asp:CheckBox ID="allChkBox" Text="Select all" runat="server" 
                  oncheckedchanged="allChkBox_CheckedChanged" /&gt;
  </code></pre>
  
  <p>I tried by doing somehting like this, but it didb't work:</p>
  
  <pre><code>bool prevSelection = false;
  protected void allChkBox_CheckedChanged(object sender, EventArgs e)
      {
  if (!prevSelection)
          {
              foreach (ListItem chkitem in CheckBoxList1.Items)
              {
                  chkitem.Selected = true;
              }
          }
          else
          {
              foreach (ListItem chkitem in CheckBoxList1.Items)
              {
                  chkitem.Selected = false;
              }
          }
          prevSelection = !prevSelection;
  }
  </code></pre>