. aspx interface
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
 
<title>datalist Control Delete operations (bulk deletion supported) </title> 
 
<script type= "Text/javascript" > 
 
function Checkall (OBJ) { 
 
var allobj = document.all; 
 
if (obj.checked)//Select All 
 
{ 
 
for (var i = 0; i < allobj.length; i++) { 
 
if (Allobj[i].type = = "checkbox") { 
 
Allobj[i].checked = true; 
 
} 
 
} 
 
} 
 
else//anti-election 
 
{ 
 
for (var i = 0; i < allobj.length; i++) { 
 
if (Allobj[i].type = = "checkbox") { 
 
allobj[i].checked = false; 
 
} 
 
} 
 
} 
 
} 
 
 
 
</script> 
 
<body> 
 
<form id= "Form1" runat= "Server" > 
 
<div> 
 
<fieldset style= "Text-align:center; width:540px; " > 
 
<legend style= "Text-align:center; "> Delete data using DataList (support for bulk deletion) </legend> 
 
 
 
<asp:datalist id= "DataList1" runat= "Server" 
 
Onitemcommand= "Datalist1_itemcommand" datakeyfield= "id" > 
 
<HeaderTemplate> 
 
<div style= "Text-align:center" > 
 
<table border = "1" cellpadding= "0" cellspacing= "0" style= "FONT-SIZE:12; width:500px "> 
 
<tr> 
 
<TD style= "width:100px" > select/Reverse <input id= "Checkbox1" type= "checkbox" Name= "All" value= "all Select" Onclick= "return Checkall (This) "title=" All selected/></td> 
 
<TD style= "width:100px" > User ID </td> 
 
<TD style= "width:100px" > User nickname </td> 
 
<TD style= "width:100px" > Personality signature </td> 
 
<TD style= "width:100px" > Delete </td> 
 
</tr> 
 
</table> 
 
</div> 
 
</HeaderTemplate> 
 
 
 
<ItemTemplate> 
 
<div style= "Text-align:center" > 
 
<table border = "1" cellpadding= "0" cellspacing= "0" style= "FONT-SIZE:12; width:500px "> 
 
<tr> 
 
<TD style= "width:100px" > <asp:checkbox id= "CheckBox2" runat= "Server"/></td> 
 
<TD style= "width:100px" ><asp:label id= "Label1" runat= "server" text= ' <%# ' Eval ("ID")%> ' ></asp: Label></td> 
 
<TD style= "width:100px" ><asp:label id= "Label2" runat= "server" text= ' <%# ' Eval ("Bg_name")%> ' ></ Asp:label></td> 
 
<TD style= "width:100px" ><asp:label id= "Label3" runat= "server" text= ' <%# ' Eval ("bg_p_autograph")%> ' ></asp:Label></td> 
 
<TD style= "width:100px" ><asp:button id= "Btndelete" runat= "server" text= "delete" commandname= "delete" 
 
Borderstyle= "None" onclientclick= "return confirm (" Confirm deletion?) ");"/></td><%--Please note the commandname command here--%> 
 
</tr> 
 
</table> 
 
</div> 
 
</ItemTemplate> 
 
<FooterTemplate> 
 
<div style= "Text-align:center" > 
 
<table border= "1" cellpadding= "0" cellspacing= "0" style= "FONT-SIZE:12PX; width:100% "> 
 
<tr> 
 
<TD style= "width:100%; Text-align:center "> 
 
<asp:button id= "Btnpldelete" runat= "server" text= "Bulk deletion" commandname= "Pldelete" 
 
Borderstyle= "None" onclientclick= "return confirm (" Confirm deletion?)  ");" /></td> 
 
</tr> 
 
</table> 
 
</div> 
 
</FooterTemplate> 
 
</asp:DataList> 
 
</fieldset> 
 
</div> 
 
</form> 
 
</body> 
 
 
 
 
. CS interface
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
 Using System; 
 
Using System.Collections.Generic; 
 
Using System.Linq; 
 
Using System.Web; 
 
Using System.Web.UI; 
 
Using System.Web.UI.WebControls; 
 
Using System.Data; 
 
Using System.Data.SqlClient; 
 
Using System.Configuration; 
 
 
  
 
 public partial class _default:system.web.ui.page
{
  
 Get the connection in the Web.config and put it in the variable
SqlConnection con = new SqlConnection (configurationmanager.connectionstrings["ConnStr"). ConnectionString);
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Calling custom methods to bind data to controls (lay the groundwork for future MVC)
Binddatalist ();
}
}
Data binding to Datelist
private void Binddatalist ()
{
Define the query statement, it is better to write SQL statements in SQL and verify that the correct copy paste over (in the query to the data, it is best to check only some of the required data do not take out, which can improve the efficiency of the operation)
String strSQL = "SELECT * from Bg_spatial";//define an SQL statement
SqlDataAdapter SDA = new SqlDataAdapter (strSQL, con);
DataSet ds = new DataSet ();
Sda. Fill (DS);//Put the executed data in the dataset
Datalist1.datasource = ds;
Datalist1.databind ();
}
 
 
protected void Datalist1_itemcommand (object source, DataListCommandEventArgs e)
{
Switch (e.commandname)
{
Single Data delete operation
Case "Delete":
Get the current DataList control column
int id = Int. Parse (Datalist1.datakeys[e.item.itemindex]. ToString ());
String strSQL = "Delete from bg_spatial where id=" + ID + "'";
if (Con. State.equals (connectionstate.closed))
{
Con. Open ()//Opens the database
}
SqlCommand cmd = new SqlCommand (strSQL, con);
if (Convert.ToInt32 (cmd). ExecuteNonQuery ()) >0)
{
Response.Write (' <script>alert (' delete success! ') </script> ");
Binddatalist ();
}
Else
{
Response.Write (' <script>alert (' delete failed! Please find the reason ') </script> ');
}
Con. Close ();//Turn off connection
Break
Bulk Data deletion operations
Case "Pldelete":
if (Con. State.equals (connectionstate.closed))
{
Con. Open ()//Opens the database
}
Datalistitemcollection dlic = datalist1.items;//Create a DataList list item collection Object
Performs a circular deletion of the selected information
for (int i = 0; i < Dlic. Count; i++)
{
if (dlic[i). ItemType = = Listitemtype.alternatingitem| | Dlic[i]. ItemType = = ListItemType.Item)
{
CheckBox Cbox = (checkbox) Dlic[i]. FindControl ("CheckBox2");
if (Cbox. Checked)
{
int p_id = Int. Parse (Datalist1.datakeys[dlic[i]. ItemIndex]. ToString ());
SqlCommand p_cmd = new SqlCommand ("Delete from bg_spatial where id=" + p_id, con);
P_cmd. ExecuteNonQuery ();
}
}
}
Con. Close ();
Binddatalist ();
Break
}
}
}
 
  
 
Run Effect chart: