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刪除數(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ào)</td>
<td style="width:100px">用戶昵稱</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("確認(rèn)刪除?");" /></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("確認(rèn)刪除?");" /></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)
{
//調(diào)用自定義方法綁定數(shù)據(jù)到控件(為以后做MVC打下基礎(chǔ))
BindDataList();
}
}
//對(duì)datelist進(jìn)行數(shù)據(jù)綁定
private void BindDataList()
{
//定義查詢語(yǔ)句,這里最好將SQL語(yǔ)句在SQL中寫好并驗(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();//打開數(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();//打開數(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編輯界面
相關(guān)文章
GridView自定義分頁(yè)實(shí)例詳解(附demo源碼下載)
這篇文章主要介紹了GridView自定義分頁(yè)的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了GridView自定義分頁(yè)所涉及的樣式布局及功能實(shí)現(xiàn)相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-03-03
asp.net 結(jié)合mysql存儲(chǔ)過(guò)程進(jìn)行分頁(yè)代碼
最近用mysql + asp.net來(lái)寫網(wǎng)站,既然mysql已經(jīng)支持存儲(chǔ)過(guò)程了,那么像分頁(yè)這么常用的東西,當(dāng)然要用存儲(chǔ)過(guò)程啦2008-11-11
.NET使用YARP通過(guò)編碼方式配置域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理
這篇文章介紹了.NET使用YARP通過(guò)編碼方式配置域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
IE下document.referrer 拒絕訪問(wèn)的解決方法
原理就是給IE瀏覽器的頁(yè)面偷偷加了個(gè)鏈接,然后自動(dòng)點(diǎn)這個(gè)鏈接,于是referrer就能保留了,感興趣的朋友可以參考下2013-09-09
Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)
這篇文章主要介紹了Asp.Net使用Bulk實(shí)現(xiàn)批量插入數(shù)據(jù)的方法,對(duì)于進(jìn)行asp.net數(shù)據(jù)庫(kù)程序設(shè)計(jì)非常有借鑒價(jià)值,需要的朋友可以參考下2014-09-09
ASP.NET UserControl 通信的具體實(shí)現(xiàn)
下面我就用ASP.NET的UserControl模擬SharePoint UserControl通信,兩者的本質(zhì),思想和實(shí)現(xiàn)方式都不變。2013-06-06
vb 中的MD5加密在asp.net中的實(shí)現(xiàn)
給定標(biāo)識(shí)哈希類型的密碼和字符串,該例程產(chǎn)生一個(gè)適合存儲(chǔ)在配置文件中的哈希密碼,感興趣的朋友可以參考下本文2013-04-04
MessagePack 和System.Text.Json 序列化和反序列化性能及對(duì)比分析
這篇文章主要介紹了MessagePack 和System.Text.Json 序列化和反序列化性能及對(duì)比分析,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

