.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(&quot;确认删除?&quot;);" /></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(&quot;确认删除?&quot;);"  /></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 之Datalist删除功能详解附代码

标签:
Datalist,删除,功能

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“ASP.Net 之Datalist删除功能详解附代码”

暂无“ASP.Net 之Datalist删除功能详解附代码”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。