主頁(yè) > 知識(shí)庫(kù) > ASP.Net 之Datalist刪除功能詳解附代碼

ASP.Net 之Datalist刪除功能詳解附代碼

熱門(mén)標(biāo)簽:電商新玩法 國(guó)美全國(guó)運(yùn)營(yíng)中心 人工智能 百度AI接口 網(wǎng)站排名優(yōu)化 電銷(xiāo)業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 客戶(hù)服務(wù)

.aspx界面

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

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刪除數(shù)據(jù)(支持批量刪除)/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">用戶(hù)編號(hào)/td>
             td style="width:100px">用戶(hù)昵稱(chēng)/td>
             td style="width:100px">個(gè)性簽名/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;確認(rèn)刪除?quot;);" />/td>%--請(qǐng)注意此處的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;確認(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()
    {

       
        //定義查詢(xún)語(yǔ)句,這里最好將SQL語(yǔ)句在SQL中寫(xiě)好并驗(yàn)證正確確在復(fù)制粘貼過(guò)來(lái)(在對(duì)數(shù)據(jù)查詢(xún)時(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ù)字分頁(yè)的實(shí)現(xiàn)方法
  • asp.net中將數(shù)據(jù)庫(kù)綁定到DataList控件的實(shí)現(xiàn)方法與實(shí)例代碼
  • ASP.NET中利用DataList實(shí)現(xiàn)圖片無(wú)縫滾動(dòng) 實(shí)例分享
  • asp.net datalist綁定數(shù)據(jù)后可以上移下移實(shí)現(xiàn)示例
  • 在ASP.NET 2.0中操作數(shù)據(jù)之三十五:使用Repeater和DataList單頁(yè)面實(shí)現(xiàn)主/從報(bào)表
  • 在ASP.NET 2.0中操作數(shù)據(jù)之三十六:在DataList里編輯和刪除數(shù)據(jù)概述
  • 在ASP.NET 2.0中操作數(shù)據(jù)之三十七:DataList批量更新
  • asp.net控件DataList分頁(yè)用法
  • 在ASP.NET 2.0中操作數(shù)據(jù)之三十九:在DataList的編輯界面里添加驗(yàn)證控件
  • 在ASP.NET 2.0中操作數(shù)據(jù)之四十:自定義DataList編輯界面

標(biāo)簽:廈門(mén) 拉薩 攀枝花 咸寧 POS機(jī) 棗莊 益陽(yáng) 南平

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.Net 之Datalist刪除功能詳解附代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266