看效果:如下:
前台js:
<script language="javascript" type="text/javascript">
var list = document.getElementsByName("ch");
window.onload =function()
{
for(var i=0;i<list.length;i++)
{
list[i].onclick=chkclick;
}
}
function chkclick()
{
var listVar="";
for(var i=0;i<list.length;i++)
{
if(list[i].checked)
{
listVar+=list[i].value+",";
}
}
document.getElementById("HiddenField1").value=listVar.subString(0,listVar.length-1);
}
function allClick()
{
var c = document.getElementById("chk");
if(c.checked)
{
var listVar="";
for(var i=0;i<list.length;i++)
{
list[i].checked=true;
listVar+=list[i].value+",";
}
document.getElementById("HiddenField1").value=listVar;//.subString(0,listVar.length-1);
}
else
{
for(var i=0;i<list.length;i++)
{
list[i].checked=false;
}
document.getElementById("HiddenField1").value="";
}
}
</script>
前台:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="全选">
<ItemTemplate>
<input name="ch" value="<%#Eval("stu_No") %>" type="checkbox" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="stu_No" HeaderText="学号" />
<asp:BoundField DataField="stu_Name" HeaderText="姓名" />
<asp:BoundField DataField="stu_Sex" HeaderText="性别" />
</Columns>
</asp:GridView>
<input id="chk" onclick="allClick()" type="checkbox" /><asp:HiddenField ID="HiddenField1"
runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
后台代码:
View Code
protected void Button1_Click(object sender, EventArgs e)
{
string s = this.HiddenField1.Value;
string[] ss = s.Split(',');
SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=Northwind;Integrated Security=True");
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "delete from 高校学生信息表 where stu_No=@stu_No";
SqlParameter sp = new SqlParameter("@stu_No",SqlDbType.NChar,10);
sp.Value = ss[i];
cmd.Parameters.Add(sp);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
databind();
}
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏