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> <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.