前台:(每个脚本函数其中有一个GridView1的ID需要更改)
<body>
<script type="text/javascript" language="javascript">
function selectAll(object)
{
table = document.all.GridView1;
var chk = table.getElementsByTagName("input");
for(var i=0;i<chk.length;i++)
{
chk[i].checked = object.checked;
}
}
function UnselectAll()
{
table = document.all.GridView1;
var chk = table.getElementsByTagName("input");
for(var i=0;i<chk.length;i++)
{
if(chk[i].id!="chkAll")
{
if(chk[i].checked)
{
chk[i].checked=!chk[i].checked;
}
else
{
chk[i].checked=true;
}
}
}
}
function checkselectAll()
{
var num=0;//未选中
table = document.all.GridView1;
var chk = table.getElementsByTagName("input");
for(var i=0;i<chk.length;i++)
{
if(!chk[i].checked)
{
num++;
}
if(num>0)
{
for(var j=0;j<chk.length;j++)
{
if(chk[j].id=="chkAll"&&chk[j].checked==true)
{
chk[j].checked=false;
}
}
}
}
}
function checkselect()
{
var count=0;//选中
table = document.all.GridView1;
var chk = table.getElementsByTagName("input");
for(var i=0;i<chk.length;i++)
{
if(chk[i].checked)
{
count++;
}
if(count==5)
{
for(var k=0;k<chk.length;k++)
{
if(chk[k].id=="chkAll"&&chk[k].checked==false)
{
chk[k].checked=true;
}
break;
}
}
}
}
</script>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" onclick="selectAll(this)" type="checkbox" name="chkAll">
<asp:LinkButton id="btnDelete" runat="server" CommandName="delet" Height="6px" CausesValidation="False">删除</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<input id="chk" onclick="checkselectAll();checkselect()" type="checkbox" value='<%#DataBinder.Eval(Container.DataItem,"id").ToString()%>' name="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<input id="chkUnSelect" title="反选" onclick="UnselectAll(this)" type="checkbox" name="反选">反选(随便放在哪里)
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//if(e.CommandName=="select")
//{
// Response.Redirect("index.aspx");
//}
#region 删除所有选中记录
if (e.CommandName == "delet")
{
System.Web.UI.HtmlControls.HtmlInputCheckBox check;
foreach (GridViewRow dgi in GridView1.Rows)
{
check = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)dgi.Cells[12].FindControl("chk"));
if (check != null)
{
if (check.Checked)
{
Response.Write("选中的值主键值为:" + check.Value + "<br>");
}
}
}
}
#endregion
}