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()
{
//定義查詢語句,這里最好將SQL語句在SQL中寫好并驗(yàn)證正確確在復(fù)制粘貼過來(在對(duì)數(shù)據(jù)查詢時(shí)最好只查所需的一些不需要的數(shù)據(jù)就不要取出,這樣可以提高運(yùn)行的效率)
string strSql = "SELECT * FROM bg_spatial";//定義一條SQL語句
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)圖片無縫滾動(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)文章
詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由
本篇文章主要介紹了詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03asp.net XMLHttpRequest實(shí)現(xiàn)用戶注冊(cè)前的驗(yàn)證
用戶注冊(cè)前的驗(yàn)證,提高用戶體驗(yàn)。2009-10-10ASP.NET自帶對(duì)象JSON字符串與實(shí)體類的轉(zhuǎn)換
這篇文章主要介紹了ASP.NET自帶對(duì)象JSON字符串與實(shí)體類的轉(zhuǎn)換,感興趣的小伙伴們可以參考一下2016-07-07asp.net 獲取Datalist中Checkbox的值的小結(jié)
最近開發(fā)過程中遇到一個(gè)小問題,要獲取checkbox的值,在網(wǎng)上搜索了一下,發(fā)現(xiàn)基本上都是用JS實(shí)現(xiàn)的,現(xiàn)在我將自己的做法記錄一下,以便以后繼續(xù)使用。2010-04-04讓Silverlight 2.0動(dòng)畫動(dòng)起來Making Silverlight 2.0 animation Start(
Microsoft Expression Blend 2 制作動(dòng)畫個(gè)人感覺倒像3DMAX 可以自動(dòng)捕捉關(guān)鍵幀2008-11-11asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法
這篇文章主要介紹了asp.net中Timer無刷新定時(shí)器的實(shí)現(xiàn)方法,是一個(gè)非常具有實(shí)用價(jià)值的技巧,需要用到Ajax技術(shù),需要的朋友可以參考下2014-08-08ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺(tái)管理系統(tǒng)之前端頁(yè)面框架構(gòu)建源碼分享
這篇文章主要為大家分享了ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后臺(tái)管理系統(tǒng)之easyui前端頁(yè)面框架構(gòu)建源碼,感興趣的小伙伴們可以參考一下2016-07-07.NET?6新特性試用之System.Text.Json功能改進(jìn)
這篇文章主要介紹了.NET?6新特性試用之System.Text.Json功能改進(jìn),2022-03-03如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
這篇文章主要給大家介紹了關(guān)于如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02