工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

看效果:如下:

前台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();
}
posted on 2011-05-10 16:24  梦里故乡  阅读(2994)  评论(4编辑  收藏  举报