Delete multiple pages in Asp.net gridview

Source: Internet
Author: User

Book_admin.aspx CopyCode The Code is as follows: <asp: dag 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 = "name">
<Itemstyle width = "60px"/>
</ASP: boundfield>
<Asp: boundfield datafield = "Comments" headertext = "comment">
<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 = "operation">
<Itemtemplate>
<Asp: checkbox runat = "server" id = "cbxid"/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
<Pagersettings mode = "nextpreviusfirstlast" firstpagetext = "first page" lastpagetext = "last page" nextpagetext = "next page" previouspagetext = "Previous Page"/>
</ASP: gridview>

Book_admin.aspx.csCopy codeThe Code is 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 whether the Administrator has logged on
Admin. checkadmin ();

Btndel. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete it? ')");

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 the delete operation
Protected void btndel_click (Object sender, eventargs E)
{
String sqltext = "(";
For (INT I = 0; I <grwbook. Rows. Count; I ++)
{
// Search for column 3 of row n
Checkbox cbx = (checkbox) grwbook. Rows [I]. findcontrol ("cbxid ");
If (cbx. Checked = true)
{
Sqltext = sqltext + convert. toint32 (grwbook. datakeys [I]. Value) + ",";
}
}

// Determine whether or not selected
If (sqltext! = "(")
{
// Remove the last comma and add parentheses
Sqltext = sqltext. substring (0, sqltext. Length-1) + ")";
Sqltext = "delete from feedback where ID in" + sqltext;
Try
{
// Execute the delete statement
DB. excutesql (sqltext );
// Rebind data
Common. salert ("deleted successfully ");
Datainit ();
// Response. Redirect ("book_admin.aspx ");
}
Catch (exception ex)
{
// If an error occurs, the error message is output.
Common. salert (ex. Message );
}
Finally
{

}
}
Else
{
Common. salert ("You have not selected a deleted item ");
}
}

}

Related Article

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.