.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使用数字分页的实现方法
  • asp.net中将数据库绑定到DataList控件的实现方法与实例代码
  • ASP.NET中利用DataList实现图片无缝滚动 实例分享
  • asp.net datalist绑定数据后可以上移下移实现示例
  • 在ASP.NET 2.0中操作数据之三十五:使用Repeater和DataList单页面实现主/从报表
  • 在ASP.NET 2.0中操作数据之三十六:在DataList里编辑和删除数据概述
  • 在ASP.NET 2.0中操作数据之三十七:DataList批量更新
  • asp.net控件DataList分页用法
  • 在ASP.NET 2.0中操作数据之三十九:在DataList的编辑界面里添加验证控件
  • 在ASP.NET 2.0中操作数据之四十:自定义DataList编辑界面
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。