Select and cancel all operations, and delete (Ajax) after selection to achieve no refreshing effect

Source: Internet
Author: User
Tags connectionstrings

 

At the moment of programming, we always need to use some of the latest technologies to solve problems .... The following is an example that I use Ajax and jequery together. Hope to help friends who study together.

 

Front-end code: <% @ page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> User Information Operation </title>
<Script language = "JavaScript" type = "text/JavaScript" src = "ajax/jquery. js"> </SCRIPT>
<Script language = "JavaScript" type = "text/JavaScript">
// Page Initialization
$ (Document). Ready (function () {loaduserinfo ();});
// Load user information to the page
Function loaduserinfo ()
{
$. Ajax (
{
Type: "Post ",
URL: "default. aspx ",
Data: {Action: 'action '},
Success: loaduserinfocallbace
}
);
}
// Page initialization callback function
Function loaduserinfocallbace (r)
{
If (r = "")
{
$ ("# Userinfo" ).html ("no data ");
}
Else
{
$ ("# Userinfo" pai.html (R );
}
}
// Select all
Function checkall (OBJ)
{
$ ("Input [@ type = checkbox] [@ name = checkitem]"). ATTR ("checked", $ (OBJ). ATTR ("checked "));
}
// Collect all selected items
Function numberid ()
{
VaR allcheckboxs = $ ("input [@ type = checkbox] [@ name = checkitem] [checked]");
VaR IDs = "";
For (I = 0; I <allcheckboxs. length; I ++)
{
VaR id = $ (allcheckboxs [I]). ATTR ("ID"). Split ("_") [1];
IDS + = ID;
IDS + = ",";
}
Return IDs;

}
// Delete a user
Function deleteuser ()
{

If (! Window. Confirm ("Do you really want to delete the selected user information? "))
{
Return;
}
VaR IDs = numberid ();
$. Ajax ({
Type: 'post ',
URL: 'default. aspx ',
Data: {Action: 'delete', userid: IDS },
Success: deleteusercall
});
}
// Delete the user callback function
Function deleteusercall (r)
{
If (r = "OK ")
{
Alert ("deleted successfully ");
Loaduserinfo ();
}
Else
{
Alert ("failed ");
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "text-align: Center" id = "userinfo">

</Div>


</Form>
</Body>
</Html>

 

 

Background code:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. text;

Public partial class _ default: system. Web. UI. Page
{
String straction = "";
Protected void page_load (Object sender, eventargs E)
{
Straction = request ["action"];
If (straction = "action ")
{
Userinfo ();
}
If (straction = "delete ")
{
Deleteuser ();
}
}
/// <Summary>
/// Zhou Xin loads user details on September 8
/// </Summary>
Public void userinfo ()
{
Sqlconnection mycon = new sqlconnection ();
Mycon. connectionstring = configurationmanager. connectionstrings ["boboconn"]. tostring ();
String SQL = "select * From loginuser ";
Sqlcommand mycom = new sqlcommand (SQL, mycon );
Mycon. open ();
Sqldatareader myda = mycom. executereader ();
Stringbuilder STR = new stringbuilder ();
Str. append ("<Table> <tr> <TD> <input id = 'checkall' name = 'checkall' type = 'checkbox' onclick = 'checkall (this) '/> select all </TD> <TD> User Name </TD> <TD> full user name </TD> </tr> ");
While (myda. Read ())
{
Str. append ("<tr> <TD> ");
Str. append ("<input id = 'checkitem _" + myda ["ID"]. tostring () + "'Type = 'checkbox' name = 'checkitem 'style = 'text-align = 'left' onclick = 'numberid () '/> </TD> ");
Str. append ("<TD>" + myda ["username"]. tostring () + "</TD> ");
Str. append ("<TD>" + myda ["fullname"]. tostring () + "</TD> </tr> ");
}
Str. append ("</table> ");
Str. append ("<Div style = 'text-align: Center'> <input type = 'button 'value = 'delete' onclick = 'deleteuser () '/> </div> ");
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write (STR );
Response. End ();
}
/// <Summary>
/// Zhou Xin 2009-6-8 Delete the details of the selected user
/// </Summary>
Public void deleteuser ()
{
// Obtain the user ID
String Strid = request ["userid"];
String userid = Strid. substring (0, Strid. Length-1 );
// Convert to an array
String [] stridarray = userid. Trim (). Split (',');
String SQL = "delete from loginuser where id = '" + stridarray [0]. tostring () + "'";
For (INT I = 0; I <stridarray. length; I ++)
{
String id = stridarray [I]. tostring ();

SQL + = "or ID = '" + ID + "'";
}
Sqlconnection mycon = new sqlconnection ();
Mycon. connectionstring = configurationmanager. connectionstrings ["boboconn"]. tostring ();
Mycon. open ();
Sqlcommand mycom = new sqlcommand (SQL, mycon );
Int n = (INT) mycom. executenonquery ();
Mycon. Close ();
If (n> 0)
{
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write ("OK ");
Response. End ();
}
Else
{
Response. Clear ();
Response. contenttype = "application/text ";
Response. Write ("no ");
Response. End ();
}
}
}

:

Related Article

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.