ASP.Net 之Datalist删除功能详解附代码

 更新时间:2013年06月13日 10:51:44   作者:  
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(&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下OnClientClick的妙用!

    asp.net下OnClientClick的妙用!

    asp.net下OnClientClick的妙用!...
    2007-04-04
  • 使用Visual Studio 2017写静态库

    使用Visual Studio 2017写静态库

    这篇文章主要为大家详细介绍了Visual Studio2017写静态库的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • .NET发送邮件的实现方法示例

    .NET发送邮件的实现方法示例

    这篇文章主要给大家介绍了关于.NET发送邮件的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-06-06
  • asp.net get set用法

    asp.net get set用法

    属性的定义和使用 属性由两个部分组成:属性头和存储器。存储器分为get访问器和set访问器。声明属性的一般形式为: 修饰符 类型 属性名
    2008-05-05
  • .NET5修改配置不重启自动生效的实现

    .NET5修改配置不重启自动生效的实现

    .NET Core,.NET5默认配置都是只加载一次,修改配置时都需要重启才能生效,如何能修改即时生效呢,本文就来介绍一下
    2021-09-09
  • asp.net中资源文件的使用

    asp.net中资源文件的使用

    .Net是一个丰富的平台,在它的结构中,程序集Assembly是自我描述的安装单元,它可以只包括一个PE(可移植可执行)格式的Dll或exe文件,也可以由多个文件组成,例如资源、Dll和exe等
    2011-12-12
  • System.Web.Routing入门及进阶

    System.Web.Routing入门及进阶

    System.Web.Routing已经作为一个程序集包含在.net3.5sp1中发布了。虽然我们并没有在3.5sp1中发现Asp.net Mvc的踪迹,但是亦以感觉到它离我们不远了
    2011-12-12
  • ASP.NET堆和栈一之基本概念和值类型内存分配

    ASP.NET堆和栈一之基本概念和值类型内存分配

    这篇文章介绍了ASP.NET堆和栈的基本概念和值类型内存分配,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • asp.net GridView排序简单实现

    asp.net GridView排序简单实现

    使用javascript操作table排序才是实用的排序,这样排序不怎么好,但是有时候可能会用来,记录一下。
    2009-12-12
  • asp.net String.format中大括号的加入方法

    asp.net String.format中大括号的加入方法

    String.format中大括号的加入方法,需要的朋友可以参考下。
    2010-05-05

最新评论