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

來源:互聯網
上載者:User

.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("確認刪除?");" /></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("確認刪除?");" /></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;
}
}
}

運行:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.