工作中点滴记录

永远保持学徒心态

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

用js代码实现在Gridview中的批量删除:

前台代码:

View Code
1 <title>无标题页</title>
2
3 <script language="javascript" type="text/javascript">
4
5 var check = document.getElementsByName("Checkbox1");
6 window.onload= function()
7 {
8 for(var i = 0; i<check.length;i++)
9 {
10 check[i].onclick=chkclick;
11 }
12 }
13 function chkclick()
14 {
15 var checkList = "";
16 for(var i = 0; i <check.length ;i++)
17 {
18 if(check[i].checked)
19 {
20 checkList=checkList +check[i].value+",";
21 }
22
23 }
24 document.getElementById("HiddenField1").value =checkList.substring(0,checkList.length -1);
25 }
26 function checkAll()
27 {
28 var checkList="";
29 var chk = document.getElementById("Checkbox11");
30 if(chk.checked)
31 {
32 for(var i =0;i<check.length ;i++)
33 {
34 check[i].checked=true;
35 checkList=checkList +check[i].value+",";
36 }
37 document.getElementById("HiddenField1").value=checkList.substring(0,checkList.length -1);
38 }
39 else
40 {
41 for(var i =0;i<check.length ;i++)
42 {
43 check[i].checked =false ;
44 document.getElementById("HiddenField1").value="";
45 }
46 }
47 }
48 </script>
49
50  </head>
51  <body>
52 <form id="form1" runat="server">
53 <div>
54 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
55 <Columns>
56 <asp:TemplateField HeaderText="全选">
57 <ItemTemplate>
58 <input name="Checkbox1" type="checkbox" value='<%#Eval("CustomerID") %>' />
59 </ItemTemplate>
60 </asp:TemplateField>
61 <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
62 <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" />
63 <asp:BoundField DataField="ContactName" HeaderText="ContactName" />
64 <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" />
65 </Columns>
66 </asp:GridView>
67 </div>
68 <asp:HiddenField ID="HiddenField1" runat="server" />
69 <asp:Button ID="Button1" runat="server" Text="Button"
70 onclick="Button1_Click" />
71 <input id="Checkbox11" type="checkbox" onclick="checkAll()" /></form>
72  </body>

后台代码:

后台代码
1 protected void Page_Load(object sender, EventArgs e)
2 {
3
4 if (!IsPostBack)
5 {
6 databind();
7 }
8 }
9 public void databind()
10 {
11 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
12 SqlCommand cmd = new SqlCommand("SELECT top 6 * FROM CUSTOMERS", con);
13 SqlDataAdapter da = new SqlDataAdapter(cmd);
14 DataSet ds = new DataSet();
15 da.Fill(ds);
16 this.GridView1.DataSource = ds.Tables[0];
17 this.GridView1.DataKeyNames = new string[] { "CustomerID", "City" };
18 this.GridView1.DataBind();
19 }
20 protected void Button1_Click(object sender, EventArgs e)
21 {
22 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
23 con.Open();
24 string strvalue = this.HiddenField1.Value;
25 string[] strArray = strvalue.Split(',');
26 for (int i = 0; i < strArray.Length; i++)
27 {
28 SqlCommand cmd = new SqlCommand();
29 cmd.Connection = con;
30 cmd.CommandText = "delete from Customers where CustomerID='" + strArray[i] + "'";
31 cmd.ExecuteNonQuery();
32 }
33 databind();
34
35 }
posted on 2011-03-09 09:14  梦里故乡  阅读(1686)  评论(0编辑  收藏  举报