asp.net gridview多頁時的大量刪除

來源:互聯網
上載者:User

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="評論">
<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="第一頁" LastPageText="末頁" NextPageText="下一頁" PreviousPageText="上一頁" />
</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)
{
//判斷管理員是否已經登陸
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();
}

//資料繫結
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();

}

//執行刪除操作
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 != "(")
{
//去掉最後的逗號,並且加上右括弧
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete from Feedback where ID in" + sqlText;
try
{
//執行刪除語句
db.excuteSql(sqlText);
//重新綁定資料
common.salert("刪除成功");
datainit();
//Response.Redirect("book_admin.aspx");
}
catch (Exception ex)
{
//若有錯誤發生,輸出錯誤資訊
common.salert(ex.Message);
}
finally
{

}
}
else
{
common.salert("您還沒有選中有刪除的項");
}
}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.