本文实例讲述了asp.net实现批量删除功能的方法。对于asp.net的学习有一定的参考价值。分享给大家供大家参考之用。具体实现方法入戏:
.aspx文件代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<asp:GridView ID= "GridView1" runat= "server" Width= "100%" EmptyDataText= "暂时无数据" BorderColor= "White" OnRowDeleting= "GridView1_RowDeleting" > <Columns> <asp:TemplateField HeaderText= "选择" > <ItemStyle Width= "20px" /> <ItemTemplate> <asp:CheckBox id= "id" runat= "Server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField= "id" HeaderText= "序号" > <ItemStyle Width= "20px" /> </asp:BoundField> <asp:TemplateField HeaderText= "标题" > <ItemStyle Width= "400px" /> <ItemTemplate> <a href= "../shangpu/<%#eval_r(" pageurl ") %>" target= "_blank" ><%#eval_r( "title" ) %></a> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText= "发表时间" > <ItemStyle Width= "100px" /> <ItemTemplate> <%# Convert.ToDateTime(eval_r( "addtime" )).Date.ToString( "yyyy-MM-dd" ) %> </ItemTemplate> </asp:TemplateField> <asp:HyperLinkField DataNavigateUrlFormatString= "shangpu_edit.aspx?id={0}" Text= "修改" NavigateUrl= "shangpu_edit.aspx?id={0}" DataNavigateUrlFields= "id" > <ItemStyle Width= "30px" /> </asp:HyperLinkField> <asp:CommandField ShowDeleteButton= "True" HeaderText= "删除" DeleteText= "<div id=" de " onclick=" JavaScript: return confirm( '确定删除吗?' ) ">删除</div>" > <ItemStyle Width= "30px" /> </asp:CommandField> </Columns> <EmptyDataTemplate> <font color=red>暂时无数据</font> </EmptyDataTemplate> <RowStyle Height= "20px" /> </asp:GridView> |
.cs 文件代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
protected void btndeleteall_Click( object sender, EventArgs e) { string sqltext = "(" ; for ( int i = 0; i < GridView1.Rows.Count; i++) { CheckBox chb = (CheckBox)GridView1.Rows[i].FindControl( "id" ); if (chb.Checked) { sqltext = sqltext + GridView1.DataKeys[i].Value.ToString() + "," ; } } sqltext = sqltext.Substring(0, sqltext.Length - 1) + ")" ; sqltext = "delete from shangpu where id in" + sqltext; string sqlcon = ConfigurationManager.AppSettings[ "ConnectionString" ].ToString(); SqlConnection con = new SqlConnection(sqlcon); con.Open(); SqlCommand cmd = new SqlCommand(sqltext, con); try { int count = Convert.ToInt32(cmd.ExecuteNonQuery()); if (count > 0) { viewbind(); MessageBox.Show( this , "删除成功,共删除" + count + "条记录!" ); } } catch { MessageBox.Show( this , "删除失败!" ); } finally { con.Close(); con.Dispose(); } } |
感兴趣的朋友可以调试运行一下本文实例,学有余力的朋友还可以对代码作出改进以完善其功能。希望本文实例对大家的asp.net学习有一定的帮助作用。