Vs 2008
This article uses two simple examples to understand ASP. NET Ajax asynchronous communication layer
The ASP. NET Ajax asynchronous communication layer provides a series of client classes for the client to request the server.
Example 1: Request the Server Page
1) requested page: ajax_getusername.aspx
Html Code Only the page declaration is left, and the rest are clear: <% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Ajax_getusername.aspx.cs " Inherits = " Ajax_getusername " %>
Ajax_getusername.aspx.cs page write processing logic: Public Partial Class Ajax_getusername: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
If (Request. querystring [ " Userid " ] = " 1 " ) {
Response. Write ("Guozhijian");
}
Else {
Response. Write (String. Empty );
}
}
}
2) client requests
<% @ 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 > Untitled page </ Title >
< Script Type = " Text/JavaScript " >
Function btngetnameclickhandler () {< br> var req = New sys. net. webrequest ();
req. set_url ( " ajax_getusername.aspx? Userid = 1 " );
req. add_completed (requestcompleted);
req. invoke ();
}
Function requestcompleted (Executor, eventargs) {
If (Executor. get_statuscode () = 200 ) {
Alert (Executor. get_responsedata ());
}
}
</ Script >
</ Head >
< Body >
< Form ID = " Form1 " Runat = " Server " >
< ASP: scriptmanager ID = " Smgr " Runat = " Server " > </ ASP: scriptmanager >
< Div >
< Input type = " Button " ID = " Btngetname " Onclick = " Btngetnameclickhandler () " Value = " Get name " />
</ Div >
</ Form >
</ Body >
</ Html >
Example 2: Request XML document
1) Create userinfos. xml
<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< Userinfos >
< Userinfo >
< Userid > 1 </ Userid >
< Username > Guozhijian </ Username >
</ Userinfo >
< Userinfo >
< Userid > 2 </ Userid >
< Username > Zhenglanzhen </ Username >
</ Userinfo >
</ Userinfos >
2) client requests
Function btngetxmlclickhandler () {< br> var req = New sys. net. webrequest ();
req. set_url ( " userinfos. " );
req. add_completed (getxmlcompleted);
req. invoke ();
}
Function getxmlcompleted (Executor, eventargs) {
If (Executor. get_statuscode () = 200 ) {
Alert (Executor. get_xml (). XML );
}
}