Microsoft ASP. NET Ajax framework (12) Introduction to the asynchronous communication layer

Source: Internet
Author: User
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 );
}
}

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.