In ASP.Net web pages, Grid View Header Checkbox select all functionality is a very common feature of modern websites, here is the script for the check all operation without post back using “Page.ClientScript.RegisterStartupScript” feature.
- Bind grid on page load with CheckAll() function.
- Call checkAll() function whenever you binding data to grid.
<asp:GridView ID=”grdBatch“ runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID=”chkall” runat=”server” />
<HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”chkSelect” runat=”server” />
<ItemTemplate>
<asp:TemplateField>
<Columns>
<asp:GridView>
public void CheckAll()
{
CheckBox chkall;
chkall = (CheckBox)grdBatch.HeaderRow.FindControl(“chkall”);
chkall.Attributes.Add(“onclick”, “checkall()”);
string str;
str = “function checkall()”;
str = str + “{if(document.getElementById(‘” + chkall.ClientID + “‘).checked==true)”;
str = str + “{“;
foreach (GridViewRow gvr in grdBatch.Rows)
{
CheckBox checkall;
checkall = (CheckBox)gvr.FindControl(“chkSelect”);
str = str + “document.getElementById(‘” + checkall.ClientID + “‘).checked=true;”;
}
str = str + “}”;
str = str + “else”;
str = str + “{“;
foreach (GridViewRow grv in grdBatch.Rows)
{
CheckBox chk1;
chk1 = (CheckBox)grv.FindControl(“chkSelect”);
str = str + “document.getElementById(‘” + chk1.ClientID + “‘).checked=false;”;
}
str = str + “}}”;
Page.ClientScript.RegisterStartupScript(GetType(), “sss”, str, true);
}
Cool