javascript : 全选checkbox
<script type="text/javascript">
function selectAll(tempControl)
{
var theBox=tempControl;
xState=theBox.checked;
elem=theBox.form.elements;
for(i=0;i<elem.length;i++)
{
if(elem[i].type=="checkbox"&&elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
{
elem[i].click();
}
}
}
}
</script>
<asp:CheckBox ID="CheckAll" runat="server" onclick="javascript:SelectAll(this);"
如果在后台写代码的形式:
protected void CheckAll_CheckedChanged(object sender, EventArgs e)
{
int intCount = GridView1.Rows.Count;
CheckBox checkall = GridView1.HeaderRow.Cells[4].FindControl("checkAll") as CheckBox;
for (int i = 0; i < intCount; i++)
{
CheckBox checkSinge = GridView1.Rows[i].Cells[4].FindControl("CheckSingle") as CheckBox;
checkSinge.Checked = checkall.Checked;
}
}