Select All checkbox in gridview


In .aspx page
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”>
<Columns>
<asp:TemplateField>
<HeaderTemplate>SelectAll<br />
<asp:CheckBox ID=”chkSelectAll” runat=”server”
AutoPostBack=”true”
OnCheckedChanged=”chkSelectAll_CheckedChanged” />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID=”chkSelect” runat=”server” />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In .aspx.cs page
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkAll =
(CheckBox)GridView1.HeaderRow.FindControl(“chkSelectAll”);
if (chkAll.Checked == true)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
CheckBox chkSel = (CheckBox) gvRow.FindControl(“chkSelect”);
chkSel.Checked = true;
}
}
else
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl(“chkSelect”);
chkSel.Checked = false;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s