在gridview外边不要放checkbox,不然当它取消选择时应该是什么功能就难以捉摸了。你可以放两个button,如下声明:
<input type="button" value="全选" onclick="<%= Get全选脚本 %>" />
<input type="button" value="反选" onclick="<%= Get反选脚本 %>" />
然后,页面的代码中实现这两个脚本(假设你的GridView的ID是GridView1,而每一行中要处理的CheckBox的ID是chb):
protected string Get反选脚本
{
get
{
string scp = "var ids=[";
foreach (GridViewRow r in this.GridView1.Rows)
{
CheckBox ch = (CheckBox)r.FindControl("chb");
if (ch != null)
scp += "'" + ch.ClientID + "',";
}
scp += "''];for(i=0;i<ids.length-1;i++) {var obj=document.getElementById(ids[i]);obj.checked=!obj.checked;};";
return scp;
}
}
protected string Get全选脚本
{
get
{
string scp = "var ids=[";
foreach (GridViewRow r in this.GridView1.Rows)
{
CheckBox ch = (CheckBox)r.FindControl("chb");
if (ch != null)
scp += "'" + ch.ClientID + "',";
}
scp += "''];for(i=0;i<ids.length-1;i++) {var obj=document.getElementById(ids[i]);obj.checked=true;};";
return scp;
}
}
转自: http://topic.youkuaiyun.com/u/20081212/16/08BD398C-92BD-432D-99C4-3723B6E2035C.html