is required that hides all the nitty-gritty details of HTTP communication and exposes additional
and higher-level controls and services.
AJAX.NET Professional (AjaxPro) is a pretty popular open-source library that adds a good
layer of abstraction over the XMLHttpRequest machinery. Written by Michael Schwarz, the
library creates proxy classes that are used by client-side JavaScript to invoke methods on the
server page. The AjaxPro framework provides full data type support and works on all common
Web browsers, including mobile devices. Nicely enough, the library can be used with
both ASP.NET 1.1 and ASP.NET 2.0.
The key tool behind the AjaxPro library is an HTTP handler that hooks up any HTTP requests
generated by the client-side part of the library:
Once the web.config file has been correctly set up, you write JavaScript functions to trigger and
control the out-of-band call. Each call targets a JavaScript object that represents the publicly
callable method on the server ASP.NET page. A client-callable method is just a public method
decorated with a specific attribute, as shown here:
[AjaxPro.AjaxMethod]
public DateTime GetCurrentTimeOnServer()
{
return DateTime.Now;
}
The class with public methods, as well as any custom types used for I/O, has to be registered
with the framework to have the corresponding JavaScript proxy created:
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(YourAjaxClass));
}
If you do this, the handler guarantees that any managed .NET object that is returned by a
server method will be serialized to a dynamically created JavaScript object to be seamlessly
used on the client. You can return any managed type, your own classes, or enum types as you
would do in plain .NET code. No view state is available during the AJAX request, meaning that
you can’t do much with page controls. In light of this, it is recommended that you create callable
methods as static methods preferably, though not necessarily, on a separate class.
AjaxPro has some key advantages over the ASP.NET Script Callback API. It uses an attribute to mark server methods that can be called from the client. This means that you have the greatest
flexibility when it comes to defining the server public interface callable from the client. In
particular, you don’t have to change the flow of the code or add new ad hoc methods just
to comply with the requested programming interface.
In addition, you can register server types for use on the client, which provides for a strongtyped
data transfer. The AjaxPro infrastructure serializes .NET types to JavaScript objects and
vice versa. The AJAX.NET hooks up and replaces the standard request processing mechanism
of ASP.NET—the page handler. As a result, you won’t receive classic ASP.NET server events
such as Init, Load, and postback. At the same time, you won’t have the view state automatically
transmitted with each out-of-band request. An AjaxPro request, though, is still processed by
the ASP.NET HTTP runtime, meaning that the request is still subject to the modules registered
with the HTTP pipeline, including session state management, roles, and authentication.
For more information about the AjaxPro library, you can take a look at http://www.ajaxpro.info.
There you will also find a link to the CodePlex Web site to get the source code of the library.