ASP.Net 之Datalist删除功能详解附代码
.aspx界面
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>datalist控件删除操作(支持批量删除)</title>
<script type="text/javascript">
function checkall(obj) {
var allobj = document.all;
if (obj.checked)//全选
{
for (var i = 0; i < allobj.length; i++) {
if (allobj[i].type == "checkbox") {
allobj[i].checked = true;
}
}
}
else//反选
{
for (var i = 0; i < allobj.length; i++) {
if (allobj[i].type == "checkbox") {
allobj[i].checked = false;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="text-align: center; width: 540px;">
<legend style=" text-align:center; ">使用datalist删除数据(支持批量删除)</legend>
<asp:datalist id="datalist1" runat="server"
onitemcommand="datalist1_itemcommand" datakeyfield="id">
<headertemplate>
<div style="text-align:center">
<table border = "1" cellpadding="0" cellspacing="0" style=" font-size:12; width:500px" >
<tr>
<td style="width:100px">全选/反选<input id="checkbox1" type="checkbox" name="全选" value="全选" onclick="return checkall(this)" title="全选" /></td>
<td style="width:100px">用户编号</td>
<td style="width:100px">用户昵称</td>
<td style="width:100px">个性签名</td>
<td style="width:100px">删除</td>
</tr>
</table>
</div>
</headertemplate>
<itemtemplate>
<div style="text-align:center">
<table border = "1" cellpadding="0" cellspacing="0" style=" font-size:12; width:500px" >
<tr>
<td style="width:100px"> <asp:checkbox id="checkbox2" runat="server" /></td>
<td style="width:100px"><asp:label id="label1" runat="server" text='<%# eval("id") %>'></asp:label></td>
<td style="width:100px"><asp:label id="label2" runat="server" text='<%# eval("bg_name") %>'></asp:label></td>
<td style="width:100px"><asp:label id="label3" runat="server" text='<%# eval("bg_p_autograph") %>'></asp:label></td>
<td style="width:100px"><asp:button id="btndelete" runat="server" text="删除" commandname="delete"
borderstyle="none" onclientclick="return confirm("确认删除?");" /></td><%--请注意此处的commandname命令--%>
</tr>
</table>
</div>
</itemtemplate>
<footertemplate>
<div style="text-align:center">
<table border="1" cellpadding="0" cellspacing="0" style="font-size:12px; width:100%">
<tr>
<td style="width:100%; text-align:center">
<asp:button id="btnpldelete" runat="server" text="批量删除" commandname="pldelete"
borderstyle="none" onclientclick="return confirm("确认删除?");" /></td>
</tr>
</table>
</div>
</footertemplate>
</asp:datalist>
</fieldset>
</div>
</form>
</body>
</html>
.cs界面
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data;
using system.data.sqlclient;
using system.configuration;
public partial class _default : system.web.ui.page
{
////得到web.config 中的连接放在变量中
sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["connstr"].connectionstring);
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
//调用自定义方法绑定数据到控件(为以后做mvc打下基础)
binddatalist();
}
}
//对datelist进行数据绑定
private void binddatalist()
{
//定义查询语句,这里最好将sql语句在sql中写好并验证正确确在复制粘贴过来(在对数据查询时最好只查所需的一些不需要的数据就不要取出,这样可以提高运行的效率)
string strsql = "select * from bg_spatial";//定义一条sql语句
sqldataadapter sda = new sqldataadapter(strsql, con);
dataset ds = new dataset();
sda.fill(ds);//把执行得到的数据放在数据集中
datalist1.datasource = ds;
datalist1.databind();
}
protected void datalist1_itemcommand(object source, datalistcommandeventargs e)
{
switch (e.commandname)
{
//单条数据删除操作
case "delete":
//取得当前datalist控件列
int id = int.parse(datalist1.datakeys[e.item.itemindex].tostring());
string strsql = "delete from bg_spatial where id='" + id + "'";
if (con.state.equals(connectionstate.closed))
{
con.open();//打开数据库
}
sqlcommand cmd = new sqlcommand(strsql, con);
if (convert.toint32(cmd.executenonquery())>0)
{
response.write("<script>alert('删除成功!')</script>");
binddatalist();
}
else
{
response.write("<script>alert('删除失败!请查找原因')</script>");
}
con.close();//关闭连接
break;
//批量数据删除操作
case "pldelete":
if (con.state.equals(connectionstate.closed))
{
con.open();//打开数据库
}
datalistitemcollection dlic = datalist1.items;//创建一个datalist列表项集合对象
//执行一个循环删除所选中的信息
for (int i = 0; i < dlic.count; i++)
{
if (dlic[i].itemtype == listitemtype.alternatingitem||dlic[i].itemtype == listitemtype.item)
{
checkbox cbox = (checkbox)dlic[i].findcontrol("checkbox2");
if (cbox.checked)
{
int p_id = int.parse(datalist1.datakeys[dlic[i].itemindex].tostring());
sqlcommand p_cmd = new sqlcommand("delete from bg_spatial where id=" + p_id , con);
p_cmd.executenonquery();
}
}
}
con.close();
binddatalist();
break;
}
}
}
运行效果图:
下一篇: asp.net 反射减少代码书写量