Check all/Selected Check box in gridview in asp.net

Html Source code 
============

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Solid" CellPadding="4" DataKeyNames="CategoryID" BorderColor="Silver" BorderWidth="1px" Width="300px">

<Columns>

<asp:TemplateField HeaderText="Categories">

<HeaderTemplate>

<asp:CheckBox ID="chkSelectAll" runat="server" Text="SelectAll" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chk1" runat="server" Text='<%# DataBinder.Eval( Container.DataItem,"categoryID" ) + " " + DataBinder.Eval( Container.DataItem,"categoryName" ) %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<HeaderStyle HorizontalAlign="Left" />

</asp:GridView>

Code Behind Source
=============


protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk;
    foreach (GridViewRow rowItem in GridView1.Rows)
    {
        chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));
        chk.Checked =((CheckBox)sender).Checked;
    }
}