asp.net下gridview 批量刪除的實現(xiàn)方法第3/3頁
更新時間:2007年11月22日 23:26:39 作者:
完整的代碼:
Default.aspx
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>gridview 批量刪除</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="vote_id" HeaderText="編號" />
<asp:BoundField DataField="vote_name" HeaderText="名稱" />
<asp:TemplateField HeaderText="選擇">
<ItemTemplate>
<asp:CheckBox id="cbxId" runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="刪除選中" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//引入命名空間
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判斷回發(fā)是否要執(zhí)行
if (!IsPostBack)
{
dataInit();
}
}
//返回一個連接對象
private SqlConnection getCon()
{
return new SqlConnection(ConfigurationManager.ConnectionStrings["voteConnectionString"].ToString());
}
//初始化數(shù)據(jù)
private void dataInit()
{
string sqlText = "select * from vote";
SqlConnection conn = getCon();
SqlDataAdapter da = new SqlDataAdapter(sqlText,conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds, "vote");
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[]{"vote_id"};
GridView1.DataBind();
conn.Close();
conn.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗號,并且加上右括號
sqlText = sqlText.Substring(0,sqlText.Length - 1) + ")";
sqlText = "delete vote where vote_id in" + sqlText;
try
{
//執(zhí)行刪除語句
SqlConnection conn = getCon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText,conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共刪除" + delCount + "條數(shù)據(jù)');</script>");
dataInit();
}
catch(Exception ex)
{
//若有錯誤發(fā)生,輸出錯誤信息
Response.Write(ex.Message);
}
}
}
相關(guān)文章
ASP.NET MVC5網(wǎng)站開發(fā)文章管理架構(gòu)(七)
繼上一篇把member的用戶部分完成,現(xiàn)在開始做文章管理部分。文章部分根據(jù)涉及顯示現(xiàn)實文章列表,發(fā)布文章,修改和刪除文章等功能。最終的實現(xiàn)目標(biāo)是使用權(quán)限來控制用戶是否能進行相應(yīng)操作,管理員權(quán)限的會顯示全部文章列表和我的文章列表,普通用戶只顯示我的文章列表2015-09-09
IIS故障(Connections_Refused)問題分析及處理
這幾天某地市Web服務(wù)器連續(xù)多次出現(xiàn)故障問題(Connections_Refused),正好借這個案例向大家詳細(xì)介紹下,需要了解的朋友可以參考下2012-12-12
.NET高級調(diào)試之sos命令輸出看不懂的處理方法
.NET高級調(diào)試屬于一個偏冷門的領(lǐng)域,國內(nèi)可觀測的資料比較少,所以很多東西需要你自己去探究源代碼,然后用各種調(diào)試工具去驗證,下面通過本文給大家分享.NET高級調(diào)試之sos命令輸出的相關(guān)知識,感興趣的朋友一起看看吧2024-03-03
ASP.NET WebAPI連接數(shù)據(jù)庫的方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET WebAPI連接數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
asp.net+jquery Gridview的多行拖放, 以及跨控件拖放
學(xué)習(xí)JQuery時,發(fā)現(xiàn)JQuery只能做單行拖放, 于是花時間做了一個多行拖放的例子, 以備以后使用。2009-11-11

