asp.net implement batch Deletion instance _ practical skills

Source: Internet
Author: User

The example of this article describes the method of ASP.net to realize the bulk deletion function. It has a certain reference value for ASP.net learning. Share for everyone to use for reference. The concrete realization method enters the scene:

The. aspx file code is as follows:

<asp:gridview id= "GridView1" runat= "Server" width= "100%" emptydatatext= "temporarily no data" bordercolor= "white" onrowdeleting= "Gridview1_rowdeleting" > <Columns> <asp:templatefield headertext= "select" > <itemstyle width= "20px"/ > <ItemTemplate> <asp:checkbox id= "id" runat= "Server"/> </ItemTemplate> </asp:tem Platefield> <asp:boundfield datafield= "id" headertext= "serial number" > <itemstyle width= "20px"/>  ndfield> <asp:templatefield headertext= "title" > <itemstyle width= "400px"/> <ItemTemplate> <a Href= ". /shangpu/<% #eval_r ("Pageurl")%> "target=" _blank "><% #eval_r (" title ")%></a> </itemtemplate > </asp:TemplateField> <asp:templatefield headertext= "Publish Time" > <itemstyle width= "100px"/> < Itemtemplate> <%# Convert.todatetime (Eval_r ("Addtime")). Date.tostring ("Yyyy-mm-dd")%> </ItemTemplate> </asp:TemplateField> &LT;asp:hyperlinkfield datanavigateurlformatstring= "shangpu_edit.aspx?id={0}" text= modify "NavigateUrl=" shangpu_ edit.aspx?id={0} "datanavigateurlfields=" id "> <itemstyle width=" 30px "/> </asp:HyperLinkField> ; Asp:commandfield showdeletebutton= "True" headertext= "delete" deletetext= "<div id=" de "onclick=" Javascript:return Confirm (' OK delete? ' "> Delete </div>" > <itemstyle width= "30px"/> </asp:CommandField> </Columns> <emptydat Atemplate> <font color=red> temporarily no data </font> </EmptyDataTemplate> <rowstyle height= "20px"/> &L
  T;/asp:gridview>

The. cs file code is as follows:

protected void Btndeleteall_click (object sender, EventArgs e)
{
string sqltext = "(";
for (int i = 0; i < GridView1.Rows.Count i++)
{
  checkbox ChB = (CheckBox) gridview1.rows[i]. FindControl ("id");
  if (ChB. Checked)
  {
 SQLText = SQLText + gridview1.datakeys[i]. Value.tostring () + ",";
  }
}
SQLText = SQLText. Substring (0, SQLText. Length-1) + ")";
SQLText = "Delete from Shangpu where id" + sqltext;
String Sqlcon = configurationmanager.appsettings["ConnectionString"]. ToString ();
SqlConnection con = new SqlConnection (Sqlcon);
Con. Open ();
SqlCommand cmd = new SqlCommand (SQLText, con);
Try
{
  int count = Convert.ToInt32 (cmd). ExecuteNonQuery ());
  if (Count > 0)
  {
 viewbind ();
 MessageBox.Show (This, "delete succeeded, delete total + count +" record!) ");
  }
}
Catch
{
  MessageBox.Show (this, "Delete failed!) ");
}
Finally
{
  con. Close ();
  Con. Dispose ();
}
}

Interested friends can debug to run the example of this article, learning more than friends can also improve the code to improve its functionality. I hope this example will help us to asp.net learning.

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.