asp.net GridView Multi-page Batch Delete _ Practical Tips

Source: Internet
Author: User
Book_admin.aspx
Copy Code code as follows:

<asp:gridview id= "Grwbook" runat= "Server" cellpadding= "4" forecolor= "#333333" gridlines= "None" Autogeneratecolumns= "false" allowpaging= "true" datakeynames= "ID" onpageindexchanging= "grwbook_pageindexchanging" Pagesize= ">"
<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"/>
<alternatingrowstyle backcolor= "White"/>
<Columns>
<asp:boundfield datafield= "id" HeaderText = "id"/>
<asp:boundfield datafield= "UserName" headertext= "name" >
<itemstyle width= "60px"/>
</asp:BoundField>
<asp:boundfield datafield= "Comments" headertext= "Comments" >
<itemstyle width= "500px"/>
</asp:BoundField>
<asp:boundfield datafield= "postdate" headertext= "date"/>
<asp:hyperlinkfield datanavigateurlfields= "NewSID" datanavigateurlformatstring= ". /news_zi.asp?id={0} "headertext=" article "datatextfield=" NewSID "target=" _blank "/>"
<asp:templatefield headertext= "Operations" >
<ItemTemplate>
<asp:checkbox runat= "Server" id= "Cbxid"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<pagersettings mode= "Nextpreviousfirstlast" firstpagetext= "first page" lastpagetext= "last" nextpagetext= "next Page" previouspagetext= "Previous page"/>
</asp:GridView>

Book_admin.aspx.cs
Copy Code code as follows:

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)
{
Determine if the administrator has logged in
Admin.checkadmin ();

BTNDEL.ATTRIBUTES.ADD ("onclick", "return Confirm" (' OK delete?) ')");

if (! Page.IsPostBack)
{
Datainit ();
}
}

protected void Grwbook_pageindexchanging (object sender, Gridviewpageeventargs e)
{
This.grwBook.PageIndex = E.newpageindex;
session["curretnpage"] = E.newpageindex;
Datainit ();
}

Data binding
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 ();

}

Perform a delete operation
protected void Btndel_click (object sender, EventArgs e)
{
String sqltext = "(";
for (int i = 0; i < GrwBook.Rows.Count; i++)
{
Search Nth Row 3 columns
CheckBox CBX = (checkbox) Grwbook.rows[i]. FindControl ("Cbxid");
if (CBX. Checked = = True)
{
SQLText = SQLText + Convert.ToInt32 (grwbook.datakeys[i). Value) + ",";
}
}

Determine if there is a check
if (SQLText!= "(")
{
Remove the last comma and add the closing parenthesis
SQLText = sqltext.substring (0, sqltext.length-1) + ")";
SQLText = "Delete from Feedback where ID" + sqltext;
Try
{
Execute DELETE statement
Db.excutesql (SQLTEXT);
rebind data
Common.salert ("delete succeeded");
Datainit ();
Response.Redirect ("book_admin.aspx");
}
catch (Exception ex)
{
If error occurs, output error message
Common.salert (ex. message);
}
Finally
{

}
}
Else
{
Common.salert ("You have not checked for deleted Items");
}
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.