First, use the client control to implement
JS:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function checkAll ()
{
Var checklist = document. getElementsByTagName ("input ");
For (var I = 0; I <checklist. length; I ++)
{
If (checklist [I]. type = "checkbox ")
{
Checklist [I]. checked = document. form1.ck. checked;
}
}
}
</Script>
GridView control:
Copy codeThe Code is as follows:
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "false">
<Columns>
<Asp: BoundField DataField = "ProductID" HeaderText = "Product NO."/>
<Asp: TemplateField>
<HeaderTemplate>
<Input id = "ck" type = "checkbox" onclick = "checkAll ();"/>
</HeaderTemplate>
<ItemTemplate>
<Asp: CheckBox ID = "checkbox1" runat = "server"/>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
Second: using server-side controls
Copy codeThe Code is as follows:
Protected void select all _ CheckedChanged (object sender, EventArgs e)
{
If (select all. Checked = true)
{
For (int I = 0; I <GridView1.Rows. Count; I ++)
{
CheckBox ck = (CheckBox) GridView1.Rows [I]. Cells [0]. FindControl ("checkbox1") as CheckBox;
If (ck! = Null)
{
Ck. Checked = true;
}
}
}
Else
{
For (int I = 0; I <GridView1.Rows. Count; I ++)
{
CheckBox ck = (CheckBox) GridView1.Rows [I]. Cells [0]. FindControl ("checkbox1") as CheckBox;
If (ck! = Null)
{
Ck. Checked = false;
}
}
}
}