Use simple methods to delete data in bulk

Source: Internet
Author: User
Tags foreach
Data on the Internet to see some examples of bulk delete data. However, most of the arrays are used to hold the selected IDs. This is more troublesome and not easy to understand. I'll take a simpler approach to this function. (see only the red part, and add a delete prompt window)

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.OleDb;
Namespace Overred.test
{
<summary>
Summary description of the T1.
</summary>
public class T1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label message;
protected System.Web.UI.WebControls.LinkButton showck;
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.LinkButton Delall;
Protected System.Web.UI.WebControls.DataGrid DG;
Private OleDbCommand cmd;
Private OleDbConnection Conn;
protected System.Web.UI.WebControls.LinkButton lb;
private string datas;


private void Page_Load (object sender, System.EventArgs e)
{


session["s"]= "DD";
Place user code here to initialize page
if (!this. Page.IsPostBack)
SQL ();





}



Private DataSet Getds ()
{
datas= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (". /overred.mdb ");
conn = new OleDbConnection (datas);
Conn. Open ();
OleDbDataAdapter ADP = new OleDbDataAdapter ("SELECT * from Subnews ORDER BY news_order DESC", conn);
DataSet ds = new DataSet ();
Adp. Fill (ds, "link");

return DS;





}
private void Sql ()
{
Alter ();
using (DataSet Dss=getds ())

THIS.DG.DATASOURCE=DSS;
This.dg.DataBind ();

}
private void Alter ()//Add Confirmation window
{
THIS.DELALL.ATTRIBUTES.ADD ("onclick", "return confirm (' OK delete ')");

}





Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.showck.Click + = new System.EventHandler (This.showck_click);
This.delall.Click + = new System.EventHandler (This.delall_click);
This.lb.Click + = new System.EventHandler (This.lb_click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Showck_click (object sender, System.EventArgs e)//Show selected items
{

ArrayList saveidchecked=new ArrayList ();
CheckBox ischecked;
Traverse each row
foreach (DataGridItem dataItem in DG. Items)
{
Get checkbox
ischecked= (checkbox) Dataitem.findcontrol ("CK");

if (ischecked.checked==true)
{
String idchecked=dataitem.cells[1]. Text;
Response.Write (idchecked);
Response.Write (ischecked.checked);
}
}


}

private void Delall_click (object sender, System.EventArgs e)
{
Alter ();
datas= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (". /overred.mdb ");
conn = new OleDbConnection (datas);
Cmd=new OleDbCommand ();
Cmd. Connection=conn;
Conn. Open ();
CheckBox ischecked;
Traverse each row
foreach (DataGridItem dataItem in DG. Items)
{
Get checkbox
ischecked= (checkbox) Dataitem.findcontrol ("CK");

if (ischecked.checked==true)//If selected then execute the delete command
{
Cmd.commandtext= "Delete from Subnews where news_id=" +dataitem.cells[1]. Text.tostring ();
Cmd. ExecuteNonQuery ();
}
}
SQL ();//rebind
Response.Redirect ("t1.aspx");
}

}
}



aspx




<%@ Page language= "C #" codebehind= "T1.aspx.cs" trace= "true" autoeventwireup= "false" inherits= "overred.



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.