asp.net implements batch deletion of instances

  • 2021-01-19 22:04:29
  • OfStack

This article illustrates how asp.net implements the bulk deletion function. For asp.net study has a certain reference value. Share with you for your reference. Specific implementation methods into the play:

The.aspx file code is as follows:


<asp:GridView ID="GridView1" runat="server" Width="100%" EmptyDataText=" Temporary data " BorderColor="White" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText=" choose ">
<ItemStyle Width="20px" />
    <ItemTemplate>
      <asp:CheckBox id="id" runat="Server" />
    </ItemTemplate>
  </asp:TemplateField>
   <asp:BoundField DataField="id" HeaderText=" The serial number " >
 <ItemStyle Width="20px" />
   </asp:BoundField>
   <asp:TemplateField HeaderText=" The title ">
 <ItemStyle Width="400px" />
 <ItemTemplate>
   <a href="../shangpu/<%#eval_r("pageurl") %>" target="_blank"><%#eval_r("title") %></a>
 </ItemTemplate>
   </asp:TemplateField>
   <asp:TemplateField HeaderText=" Published 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 the " 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(' Are you sure to delete it? ')"> delete </div>" >
 <ItemStyle Width="30px" />
   </asp:CommandField>
 </Columns>
 <EmptyDataTemplate>
 <font color=red> Temporary data </font>
 </EmptyDataTemplate>
<RowStyle Height="20px" />
  </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 in" + 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 the success , delete " + count + " Record! ");
  }
}
catch
{
  MessageBox.Show(this, " Delete failed! ");
}
finally
{
  con.Close();
  con.Dispose();
}
}

If you are interested, you can debug and run the examples in this article. If you are able to learn, you can also make improvements to the code to improve its functionality. I hope the examples in this paper will be helpful to your asp.net learning.


Related articles: