The first: Using client-side controls to implement
Js:
Copy Code code 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 Code code as follows:
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "false" >
<Columns>
<asp:boundfield datafield= "ProductID" headertext= "Product number"/>
<asp:TemplateField>
<HeaderTemplate>
<input id= "ck" type= "checkbox" onclick= "Checkall ();"/>
</HeaderTemplate>
<ItemTemplate>
<asp:checkbox id= "CheckBox1" runat= "Server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The second: Using server-side controls to implement
Copy Code code 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) Gri Dview1.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;
}
}
}
}