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