Ajax Architecture
1. intitial XMLHTTPRequest object and send the request;
2. Assign the process function for the request;
3. Send out the HTTP request;
4. process the return result from server.
Ajax example:
<Script language = "JavaScript" runat = Server>
VaR http_request_check = false;
Function send_request_check (URL) {// initialize, specify the processing function, and send the request Function
Http_request_check = false;
// Start initializing the XMLHTTPRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request_check = new XMLHttpRequest ();
If (http_request_check.overridemimetype) {// set the mime category
Http_request_check.overridemimetype ('text/xml ');
}
}
Else if (window. activexobject) {// IE browser
Try {
Http_request_check = new activexobject ("msxml2.xmlhttp ");
} Catch (E)
{
Try {
Http_request_check = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request_check) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}
Http_request_check.onreadystatechange = processrequest_check;
// Determine the request sending method and URL and whether to execute the next part synchronously Code
Http_request_check.open ("get", URL, true );
Http_request_check.send (null );
}
// Function for processing the returned information
Function processrequest_check (){
If (http_request_check.readystate = 4) {// judge the object status
If (http_request_check.status = 200) {// The information has been returned successfully. Start to process the information.
// Document. getelementbyid ('returnstr'). innertext = http_request.responsetext;
Alert (http_request_check.responsetext );
If (http_request_check.responsetext = "User exists ")
Document. form1.submit1. Disabled = true;
Else
Document. form1.submit1. Disabled = false;
} Else {// The page is abnormal.
Alert ("the page you requested has an exception. ");
}
}
}
Function usercheck (){
VaR F = Document. form1;
VaR username = f. username. value;
If (username = ""){
Window. Alert ("the user name cannot be blank. ");
F. username. Focus ();
Return false;
}
Else {
Send_request_check ("ajax-1-2.aspx? Username = "+ username +" & method = check ");
}
}
</SCRIPT>
// Ajax-1-2.aspx Codes
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
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. xml;
Public partial class ajax_1_2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String method = request ["method"]. tostring ();
If (Method = "check ")
Check ();
Else if (Method = "show ")
Show ();
}
Public void check ()
{
String username = request ["username"]. tostring ();
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("user. config "));
Xmlnodelist nodelist = Doc. selectsinglenode ("users"). childnodes;
Foreach (xmlnode Xn in nodelist)
{
Xmlelement Xe = (xmlelement) xn;
If (Xe. getattribute ("login"). tostring () = username)
{
Response. Write ("User existes ");
Return;
}
}
Response. Write ("You can use the name ");
}
Public void show ()
{
String OBJ = request ["OBJ"];
Xmldocument Doc = new xmldocument ();
Doc. Load (server. mappath ("user. config "));
Xmlnodelist nodelist = Doc. selectsinglenode ("users"). childnodes;
If (obj. tostring () = "showadmin ")
{
Foreach (xmlnode Xn in nodelist)
{
Xmlelement Xe = (xmlelement) xn;
If (Xe. getattribute ("rights"). tostring () = "admin ")
Response. Write ("--" + Xe. getattribute ("login"). tostring () + "<br> ");
Xmlnodelist nodelist1 = xn. childnodes;
Foreach (xmlnode xe1 in nodelist1)
Response. Write ("----" + xe1.name + "=" + xe1.innertext. tostring () + "& nbsp ");
Response. Write ("<br> ");
}
}
Else if (obj. tostring () = "showuser ")
{
Try
{
Sqlconnection conn = new sqlconnection ();
Conn. connectionstring = "Data Source = (local); initial catalog = ezplanning; user id = sa_autolink; Pwd = q1234 % TT ";
Conn. open ();
Sqlcommand cmd = new sqlcommand ();
Cmd. Connection = conn;
Cmd. commandtype = commandtype. text;
Cmd. commandtext = "select username from autolink_user ";
Sqldatareader RD = cmd. executereader ();
While (RD. Read ())
Response. Write ("--" + RD ["username"]. tostring () + "<br> ");
Conn. Close ();
Cmd. Dispose ();
}
Catch (exception E)
{
Response. Write (E. Message );
}
}
}
}