asp.net gridview多頁(yè)時(shí)的批量刪除
更新時(shí)間:2008年07月03日 08:58:19 作者:
多余的代碼我就不貼了,有段時(shí)間沒(méi)寫(xiě).net了,最近又開(kāi)始寫(xiě)了,結(jié)果就一個(gè)gridview含多頁(yè)的批量刪除弄了我很久。貼上代碼,忘記再看下:
book_admin.aspx
<asp:GridView ID="grwBook" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="ID" OnPageIndexChanging="grwBook_PageIndexChanging" PageSize="12">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText = "ID" />
<asp:BoundField DataField="UserName" HeaderText="姓名">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="Comments" HeaderText="評(píng)論">
<ItemStyle width="500px" />
</asp:BoundField>
<asp:BoundField DataField="Postdate" HeaderText="日期" />
<asp:HyperLinkField DataNavigateUrlFields="newsid" DataNavigateUrlFormatString="../news_zi.asp?id={0}" HeaderText="文章" DataTextField="newsid" target="_blank" />
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:CheckBox runat="Server" ID="cbxId" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="第一頁(yè)" LastPageText="末頁(yè)" NextPageText="下一頁(yè)" PreviousPageText="上一頁(yè)" />
</asp:GridView>
book_admin.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.OleDb;
public partial class admin_book_admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判斷管理員是否已經(jīng)登陸
admin.checkadmin();
btnDel.Attributes.Add("onclick", "return confirm('確定刪除嗎?')");
if (!Page.IsPostBack)
{
datainit();
}
}
protected void grwBook_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.grwBook.PageIndex = e.NewPageIndex;
Session["curretnPage"] = e.NewPageIndex;
datainit();
}
//數(shù)據(jù)綁定
private void datainit()
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,newsid from Feedback order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//執(zhí)行刪除操作
protected void btnDel_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)grwBook.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(grwBook.DataKeys[i].Value) + ",";
}
}
//判斷是否有選中
if (sqlText != "(")
{
//去掉最后的逗號(hào),并且加上右括號(hào)
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete from Feedback where ID in" + sqlText;
try
{
//執(zhí)行刪除語(yǔ)句
db.excuteSql(sqlText);
//重新綁定數(shù)據(jù)
common.salert("刪除成功");
datainit();
//Response.Redirect("book_admin.aspx");
}
catch (Exception ex)
{
//若有錯(cuò)誤發(fā)生,輸出錯(cuò)誤信息
common.salert(ex.Message);
}
finally
{
}
}
else
{
common.salert("您還沒(méi)有選中有刪除的項(xiàng)");
}
}
}
復(fù)制代碼 代碼如下:
<asp:GridView ID="grwBook" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" AllowPaging="true" DataKeyNames="ID" OnPageIndexChanging="grwBook_PageIndexChanging" PageSize="12">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText = "ID" />
<asp:BoundField DataField="UserName" HeaderText="姓名">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="Comments" HeaderText="評(píng)論">
<ItemStyle width="500px" />
</asp:BoundField>
<asp:BoundField DataField="Postdate" HeaderText="日期" />
<asp:HyperLinkField DataNavigateUrlFields="newsid" DataNavigateUrlFormatString="../news_zi.asp?id={0}" HeaderText="文章" DataTextField="newsid" target="_blank" />
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:CheckBox runat="Server" ID="cbxId" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="第一頁(yè)" LastPageText="末頁(yè)" NextPageText="下一頁(yè)" PreviousPageText="上一頁(yè)" />
</asp:GridView>
book_admin.aspx.cs
復(fù)制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.OleDb;
public partial class admin_book_admin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判斷管理員是否已經(jīng)登陸
admin.checkadmin();
btnDel.Attributes.Add("onclick", "return confirm('確定刪除嗎?')");
if (!Page.IsPostBack)
{
datainit();
}
}
protected void grwBook_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.grwBook.PageIndex = e.NewPageIndex;
Session["curretnPage"] = e.NewPageIndex;
datainit();
}
//數(shù)據(jù)綁定
private void datainit()
{
DataSet ds = db.dataSet("select ID,UserName,Comments,Postdate,newsid from Feedback order by Postdate desc,newsid desc");
if (ViewState["currentPage"] != null)
{
this.grwBook.PageIndex = Convert.ToInt32(Session["currentPage"]);
}
this.grwBook.DataSource = ds;
this.grwBook.DataBind();
}
//執(zhí)行刪除操作
protected void btnDel_Click(object sender, EventArgs e)
{
string sqlText = "(";
for (int i = 0; i < grwBook.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)grwBook.Rows[i].FindControl("cbxId");
if (cbx.Checked == true)
{
sqlText = sqlText + Convert.ToInt32(grwBook.DataKeys[i].Value) + ",";
}
}
//判斷是否有選中
if (sqlText != "(")
{
//去掉最后的逗號(hào),并且加上右括號(hào)
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete from Feedback where ID in" + sqlText;
try
{
//執(zhí)行刪除語(yǔ)句
db.excuteSql(sqlText);
//重新綁定數(shù)據(jù)
common.salert("刪除成功");
datainit();
//Response.Redirect("book_admin.aspx");
}
catch (Exception ex)
{
//若有錯(cuò)誤發(fā)生,輸出錯(cuò)誤信息
common.salert(ex.Message);
}
finally
{
}
}
else
{
common.salert("您還沒(méi)有選中有刪除的項(xiàng)");
}
}
}
相關(guān)文章
IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證(快速入門(mén))
這篇文章主要介紹了IdentityServer4實(shí)現(xiàn).Net Core API接口權(quán)限認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
docker部署Asp.net core應(yīng)用的完整步驟
這篇文章主要給大家介紹了關(guān)于docker部署Asp.net core應(yīng)用的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
ASP.NET中根據(jù)XML動(dòng)態(tài)創(chuàng)建使用WEB組件
ASP.NET中根據(jù)XML動(dòng)態(tài)創(chuàng)建使用WEB組件...2006-09-09
asp.net Cookie值中文亂碼問(wèn)題解決方法
cookie里面不能寫(xiě)中文,是由于cookie先天的編碼方式造成的,所以有必要存在一種中間的編碼方式:URLEncode是最好的選擇,感興趣的你可千萬(wàn)不要錯(cuò)過(guò)了哈,或許本文提供的知識(shí)點(diǎn)對(duì)你學(xué)習(xí)cookie有所幫助2013-02-02

