ASP.NET 'CheckAllCB' checkbox header template does not work
<p>I am using visual studio 2005 c#, and doing server side coding. </p>

<p>I have a checkbox template in my gridview. I have tried to assign a <code>CheckAllCB</code> button outside the gridview, so that when<code>CheckAllCB</code> is clicked, the list of checkboxes in the item template will be checked. 
However, it does not work and I am not able to spot the mistake. 
Below is my code for my <code>CheckAllCB</code> button:</p>

<pre><code>protected void CheckAllCB_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk = (CheckBox)GridView1.HeaderRow.FindControl("CheckAll");
    if (chk.Checked)
    {
        for (int i = 0; i &lt; GridView1.Rows.Count; i++)
        {
            CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("UserSelector");
            chkrow.Checked = true;
        }

    }
    else
    {
        for (int i = 0; i &lt; GridView1.Rows.Count; i++)
        {
            CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("UserSelector");
            chkrow.Checked = false;
        }
    }
}
</code></pre>

<p><strong>(SOLVED)</strong></p>

<hr>

<p>Thus I tried using a checkbox checkchange in the header template. However, when the checkbox <code>CheckAll</code> in header template is check changed, nothing happens in the checkboxes in the itemtemplate. </p>

<p>Below is my code for <code>CheckAllCB</code> in header template</p>

<pre><code>private void ToggleCheckState(bool checkState)
{
    // Iterate through the Products.Rows property
    foreach (GridViewRow row in GridView1.Rows)
    {
        // Access the CheckBox
        CheckBox cb = (CheckBox)row.FindControl("UserSelector");
        if (cb != null)
            cb.Checked = checkState;
    }
}
protected void CheckAll_Click(object sender, EventArgs e)
{
    ToggleCheckState(true);
}
protected void UncheckAll_Click(object sender, EventArgs e)
{
    ToggleCheckState(false);
}
</code></pre>

<p>Anyone can help me identify the mistake I did in my method? Thank you</p>

<hr>

<p><code>UserSelection</code> item template:
<img src="http://i.stack.imgur.com/1PqTo.png" alt="enter image description here"></p>

<p><code>CheckAllCB</code> header template:
<img src="http://i.stack.imgur.com/fzCXG.png" alt="enter image description here"></p>

<hr>

<p>Added source code for <code>GridView</code>:</p>

<pre><code>&lt;asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" ondatabound="gv_DataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"&gt;
    &lt;Columns&gt;
                     &lt;asp:TemplateField&gt;
                     &lt;HeaderTemplate&gt;
                         &lt;asp:CheckBox ID="CheckAllCB" runat="server" OnCheckedChanged="CheckAllCB_CheckedChanged" /&gt;
  &lt;/HeaderTemplate&gt;
                        &lt;ItemTemplate&gt;

                          &lt;asp:CheckBox ID="UserSelection" OnCheckedChanged="UserSelector_CheckedChanged" runat="server" /&gt;

                        &lt;/ItemTemplate&gt;
                    &lt;/asp:TemplateField&gt;
                &lt;/Columns&gt;
&lt;/asp:GridView&gt;
</code></pre>
