Use ajaxpro to implement Ajax

Source: Internet
Author: User
In the previous article about how to implement Ajax using XMLHttpRequest, we introduced how to implement Ajax using XMLHttpRequest. In this article, we will use the ajaxpro. NET framework to implement the same function: Obtain the server time asynchronously on the client! Ajaxpro. NET is an excellent Ajax framework in the. NET environment. Its usage is very simple. You can refer to the relevant materials and briefly describe the following points:
1) Add the ajaxpro. dll application;
2) Configure web. config;
3) write the server method and add the [ajaxpro. ajaxmethod] statement;
4) The client calls the server method. (the biggest advantage of ajaxpro is that the client directly calls the server method in JS)
Client Code : < Html Xmlns = "Http://www.w3.org/1999/xhtml"   >
< Head Runat = "Server" >
< Title > Ajaxpro onloading </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div ID = "Loadinfo" Style = "Visibility: hidden; position: absolute; left: 0px; top: 0px; Background-color: red; color: White ;" > Loading </ Div >

< Input ID = "Button1" Type = "Button" Value = "Get servertime" Onclick = "Javascript: gettime (); void (0 )"   />

< Script Type = "Text/JavaScript" Defer = "Defer" >

// Loading Effect
Ajaxpro. onloading =   Function (B)
{
VaR A = Document. getelementbyid ( " Loadinfo " );
A. style. Visibility = B ?   " Visible " : " Hidden " ;
}

Function Gettime ()
{
// Call the server Method
// Call method: Class Name. Method Name (the parameter specifies a callback function)
Myload. getservertime (callback );
}

Function Callback (RES) // Callback Function to display results
{
Alert (res. value );
}
</ Script >
</ Form >
</ Body >
</ Html >

Note: place the JavaScript code at the end of the form to ensure that the ajaxpro object can be accessed; otherwise, the ajaxpro is prompted to be undefined, because the ajaxpro object is not found until JS is output to the page on the page_load server; and JavaScript defer = "Defer", this code is not executed when the page is loaded.
Server code: Public Partial Class Myload: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (Typeof(Myload ));//Register ajaxpro. The parameters in the brackets are the current class name.
}

[Ajaxpro. ajaxmethod] // Declare the ajaxpro Method
Public   String Getservertime ()
{
System. Threading. thread. Sleep (2000);
ReturnDatetime. Now. tostring ();
}
}

The onloading Effect of page loading implemented by ajaxpro. NET is as follows:

Download the sample code:/files/chy710/ajaxpro.rar
Environment: ajaxpro2.dll 6.10.6.2 ASP. net2.0

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.