Try this
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnChildCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Item" />
<asp:BoundField DataField="Price" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<hr />
<asp:UpdatePanel ID="up2" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnChildCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Item" />
<asp:BoundField DataField="Price" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Price") });
dt.Rows.Add("Shirt", 199);
dt.Rows.Add("Football", 020);
dt.Rows.Add("Shirt", 566);
dt.Rows.Add("Disc", 099);
dt.Rows.Add("Watch", 54);
dt.Rows.Add("Clock", 890);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView2.DataSource = dt;
GridView2.DataBind();
}
}
protected void OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
GridView gv = chk.NamingContainer.Parent.Parent as GridView;
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
(row.FindControl("CheckBox1") as CheckBox).Checked = chk.Checked;
}
}
}
protected void OnChildCheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (sender as CheckBox);
GridView gv = chk.NamingContainer.Parent.Parent as GridView;
bool isAllChecked = true;
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (!(row.FindControl("CheckBox1") as CheckBox).Checked)
{
isAllChecked = false;
break;
}
}
}
CheckBox chkAll = (gv.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = isAllChecked;
}