溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.Net中怎么利用Datalist實(shí)現(xiàn)刪除功能

發(fā)布時(shí)間:2021-07-16 14:07:48 來(lái)源:億速云 閱讀:171 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹ASP.Net中怎么利用Datalist實(shí)現(xiàn)刪除功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

.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 >
     <legend >使用Datalist刪除數(shù)據(jù)(支持批量刪除)</legend>

        <asp:DataList ID="DataList1" runat="server"
             onitemcommand="DataList1_ItemCommand" DataKeyField="id">
        <HeaderTemplate>
        <div >
        <table border = "1" cellpadding="0" cellspacing="0"    >
         <tr>
             <td >全選/反選<input id="Checkbox1" type="checkbox" name="全選" value="全選" onclick="return CheckAll(this)" title="全選" /></td>
             <td >用戶編號(hào)</td>
             <td >用戶昵稱</td>
             <td >個(gè)性簽名</td>
             <td >刪除</td>
         </tr>
        </table>
        </div>
        </HeaderTemplate>

            <ItemTemplate>
            <div >
            <table border = "1" cellpadding="0" cellspacing="0"    >
                 <tr>
                 <td > <asp:CheckBox ID="CheckBox2" runat="server" /></td>
                 <td ><asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label></td>
                 <td ><asp:Label ID="Label2" runat="server" Text='<%# Eval("bg_name") %>'></asp:Label></td>
                 <td ><asp:Label ID="Label3" runat="server" Text='<%# Eval("bg_p_autograph") %>'></asp:Label></td>
                 <td ><asp:Button ID="btnDelete" runat="server" Text="刪除"  CommandName="delete"
                        BorderStyle="None" onclientclick="return confirm(&quot;確認(rèn)刪除?&quot;);" /></td><%--請(qǐng)注意此處的CommandName命令--%>
                </tr>
             </table>
             </div>
            </ItemTemplate>
            <FooterTemplate>
                 <div >
                     <table border="1" cellpadding="0" cellspacing="0" >
                         <tr>
                         <td >
                             <asp:Button ID="btnPLDelete" runat="server" Text="批量刪除"  CommandName="pldelete"
                                  BorderStyle="None" onclientclick="return confirm(&quot;確認(rèn)刪除?&quot;);"  /></td>
                         </tr>
                     </table>
                 </div>
            </FooterTemplate>
        </asp:DataList>
        </fieldset>
     </div>
     </form>
 </body>
 </html>

.cs界面

復(fù)制代碼 代碼如下:


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)
        {
           //調(diào)用自定義方法綁定數(shù)據(jù)到控件(為以后做MVC打下基礎(chǔ))
            BindDataList();
        }
    }
    //對(duì)datelist進(jìn)行數(shù)據(jù)綁定
    private void BindDataList()
    {

       
        //定義查詢語(yǔ)句,這里最好將SQL語(yǔ)句在SQL中寫(xiě)好并驗(yàn)證正確確在復(fù)制粘貼過(guò)來(lái)(在對(duì)數(shù)據(jù)查詢時(shí)最好只查所需的一些不需要的數(shù)據(jù)就不要取出,這樣可以提高運(yùn)行的效率)
        string strSql = "SELECT * FROM bg_spatial";//定義一條SQL語(yǔ)句
        SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);//把執(zhí)行得到的數(shù)據(jù)放在數(shù)據(jù)集中
        DataList1.DataSource = ds;
        DataList1.DataBind();

    }


    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            //單條數(shù)據(jù)刪除操作
            case "delete":
                //取得當(dāng)前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();//打開(kāi)數(shù)據(jù)庫(kù)
                }
                SqlCommand cmd = new SqlCommand(strSQL, con);
                if (Convert.ToInt32(cmd.ExecuteNonQuery())>0)
                {
                    Response.Write("<script>alert('刪除成功!')</script>");
                    BindDataList();
                }
                else
                {
                    Response.Write("<script>alert('刪除失敗!請(qǐng)查找原因')</script>");
                }
                con.Close();//關(guān)閉連接
                break;
            //批量數(shù)據(jù)刪除操作
            case "pldelete":
                if (con.State.Equals(ConnectionState.Closed))
                {
                    con.Open();//打開(kāi)數(shù)據(jù)庫(kù)
                }
                DataListItemCollection dlic = DataList1.Items;//創(chuàng)建一個(gè)DataList列表項(xiàng)集合對(duì)象
                //執(zhí)行一個(gè)循環(huán)刪除所選中的信息
                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;
        }
    }
}

運(yùn)行效果圖:

ASP.Net中怎么利用Datalist實(shí)現(xiàn)刪除功能

關(guān)于ASP.Net中怎么利用Datalist實(shí)現(xiàn)刪除功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI