Detailed explanation of Datalist deletion function of ASP. Net with code

Source: Internet
Author: User

. Aspx Interface

Copy codeThe Code is as follows: <Head runat = "server">
<Title> DataList control deletion operation (batch 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 // reselect
{
For (var I = 0; I <AllObj. length; I ++ ){
If (AllObj [I]. type = "checkbox "){
AllObj [I]. checked = false;
}
}
}
}

</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Fieldset style = "text-align: center; width: 540px;">
<Legend style = "text-align: center;"> Use Datalist to delete data (batch deletion is supported) </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 all/invert <input id =" Checkbox1 "type =" checkbox "name =" select all "value =" select all "onclick =" return CheckAll (this) "title =" select all "/> </td>
<Td style = "width: 100px"> User ID </td>
<Td style = "width: 100px"> user nickname </td>
<Td style = "width: 100px"> personalized 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 (" are you sure you want to delete? ");"/> </Td> <% -- 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 = "batch Delete" CommandName = "pldelete"
BorderStyle = "None" onclientclick = "return confirm (" are you sure you want to delete? ");"/> </Td>
</Tr>
</Table>
</Div>
</FooterTemplate>
</Asp: DataList>
</Fieldset>
</Div>
</Form>
</Body>
</Html>

. Cs Interface

Copy codeThe Code is 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 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)
{
// Call a custom method to bind data to the control (laying the foundation for future MVC)
BindDataList ();
}
}
// Bind datelist data
Private void BindDataList ()
{

// Define the query statement, it is best to write the SQL statement in the SQL statement and verify that the SQL statement is correctly copied and pasted. (when querying data, it is best not to retrieve the unnecessary data, this improves the running efficiency)
String strSql = "SELECT * FROM bg_spatial"; // defines an SQL statement.
SqlDataAdapter sda = new SqlDataAdapter (strSql, con );
DataSet ds = new DataSet ();
Sda. Fill (ds); // put the executed data in the data set
DataList1.DataSource = ds;
DataList1.DataBind ();

}

Protected void DataList1_ItemCommand (object source, DataListCommandEventArgs e)
{
Switch (e. CommandName)
{
// Delete a single data entry
Case "delete ":
// Obtain 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 (); // Open the database
}
SqlCommand cmd = new SqlCommand (strSQL, con );
If (Convert. ToInt32 (cmd. ExecuteNonQuery ()> 0)
{
Response. Write ("<script> alert ('deleted successfully! ') </Script> ");
BindDataList ();
}
Else
{
Response. Write ("<script> alert ('deletion failed! Find the reason ') </script> ");
}
Con. Close (); // Close the connection
Break;
// Delete batch data
Case "pldelete ":
If (con. State. Equals (ConnectionState. Closed ))
{
Con. Open (); // Open the database
}
DataListItemCollection dlic = DataList1.Items; // create a DataList list item set object
// Execute a loop to delete 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_2.16.executenonquery ();
}
}

}
Con. Close ();
BindDataList ();
Break;
}
}
}

Run:

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.