A few days ago, Jeffrey Zhao said that using updatepanel in ASP. NET Ajax does not seem to be good. I thought about it, but it was just a bit like a high-definition gun hitting a mosquito. Some friends hope that I can take care of Ajax. Net professional in comparison, which is also a very good Ajax framework on the ASP. NET platform.
This article is formed under various requirements. This article compares the implementation of client-side method calls on the server-side in the Ajax framework of ASP. NET Ajax (Atlas), anthem. net, and Ajax. Net professional. The client-side method to call the server-side method is an issue that must be considered by every Ajax framework and is very representative. InProgramDuring the compilation process, I will also discard the "smart" server-side controls (such as ASP. net Ajax updatepanel), which is fully implemented using manual JavaScript and C #, and strives to create a more "fair" comparison environment.
This article focuses on comparison rather than introduction. You can downloadCode.
Functions to be implemented by the program
The program function is simple, but it is also typical:
- The user clicks a <input> button on the page.
- The Browser executes the client JavaScript event processing function of this button.
- The javascript event processing function calls a server-side method.
- Server-side method execution and return the current time
- The client JavaScript callback function is executed to display the time returned by the server in a <span> page.
Program running interface
Since the interfaces, behaviors, and implementation functions of these three programs are completely consistent, Here we only use the ASP. NET Ajax (Atlas) version as an example. The following is the interface after program initialization:
Click the button. The five steps described above are executed in sequence. The result shown in is displayed as follows:
Implementation Code of ASP. NET Ajax (Atlas)
The first is the HTML part of the page. Pay attention to the unique scriptmanager control of ASP. NET Ajax (Atlas:
<ASP: scriptmanager ID= "Scriptmanager1" Runat= "Server" />
<Div>
<Input Type= "Button" Value= "Get server time" ID= "Btngetservertime" Onclick= "Return btngetservertime_onclick ()" />
<Span ID= "Result" />
</Div>
Next, the Javascript part of the client. Use pagemethods. getservertime () to call the server method:
FunctionBtngetservertime_onclick (){
Pagemethods. getservertime (cb_getservertime );
}
FunctionCb_getservertime (result ){
Document. getelementbyid ("Result"). Innerhtml = result;
}
The server code is as follows. Note that the method must be static and the attributes [system. Web. Services. webmethod] and [microsoft. Web. Script. Services. scriptmethod] must be added:
[System. Web. Services. webmethod]
[Microsoft. Web. Script. Services. scriptmethod]
Public Static StringGetservertime ()
{
ReturnDatetime. Now. tostring ();
}
OK!
Implementation Code of the anthem. Net version
The first is the HTML part of the page. There is nothing to pay attention:
<Div>
<Input Type= "Button" Value= "Get server time" ID= "Btngetservertime" Onclick= "Return btngetservertime_onclick ()" />
<Span ID= "Result" />
</Div>
In the Javascript section of the client, use the anthem_invokepagemethod auxiliary function to call the server method:
FunctionBtngetservertime_onclick (){
Anthem_invokepagemethod ("Getservertime", [], Cb_getservertime );
}
FunctionCb_getservertime (result ){
Document. getelementbyid ("Result"). Innerhtml = result. value;
}
Finally, the server-side code should be registered in page_load (similar to scriptmanager in ASP. NET Ajax ). The [Anthem. Method] attribute is also applied to methods:
[Anthem. Method]
Public StringGetservertime ()
{
ReturnDatetime. Now. tostring ();
}
Protected VoidPage_load (ObjectSender, eventargs E)
{
Anthem. Manager. Register (This);
}
OK!
Implementation Code of Ajax. net Professional
The HTML part of the page is exactly the same as that of the anthem. Net version:
<Div>
<Input Type= "Button" Value= "Get server time" ID= "Btngetservertime" Onclick= "Return btngetservertime_onclick ()" />
<Span ID= "Result" />
</Div>
The javascript part on the client is somewhat similar to ASP. NET Ajax. Note that "ASP. ajaxpro_aspx" is the compiled class name on the ASP. NET page:
FunctionBtngetservertime_onclick (){
ASP. ajaxpro_aspx.getservertime (cb_getservertime );
}
FunctionCb_getservertime (result ){
Document. getelementbyid ("Result"). Innerhtml = result. value;
}
The server code is similar to the implementation method of the anthem. Net version. You also need to register the code in page_load and apply an attribute for the method-but here is [ajaxpro. ajaxmethod]:
[Ajaxpro. ajaxmethod]
Public StringGetservertime ()
{
ReturnDatetime. Now. tostring ();
}
Protected VoidPage_load (ObjectSender, eventargs E)
{
Ajaxpro. Utility. registertypeforajax (This. GetType ());
}
OK!
Comparison of implementation methods
Let's talk with charts. I will not comment again:
Source codeDownload
This is the source code of the program I used in this experiment: 3aspnetajaxframeworkstest.zip