"Atlas" Foundations: ASP.NET 2.0 Callback
One of the issues developers had when they first started to develop commercial Web sites was some of the limitations of using a browser as the interface. For instance, there were many cases where you wanted to retrieve information from the server after the user had performed some action, like entering an employee number in a Web page to retrieve the details of an employee. To accomplish this, you would post the current page to the server, retrieve the employee information from the database and refresh the page with the information retrieved from the server. Although this method of refreshing the whole page is very common today, it is inefficient because the Web page refreshes and re-renders the entire page of content, even if only a small percentage of the page has actually changed.
Fortunately ASP.NET 2.0 provides an efficient approach to invoke a remote function from a server page without refreshing the browser. This new feature is called ASP.NET 2.0 Script Callback, and it builds on the foundation of the
XmlHttp object library. Using ASP.NET 2.0 script callback feature, you can emulate some of the behaviors of a traditional fat-client application in a Web-based application. It can be used to refresh individual controls, to validate controls, or even to process a form without having to post the whole page to the server. When you utilize script callback approach to retrieve data, you would typically transfer the data in the form of XML stream from the server side to the client and then load the XML data in a client-side XML DOM object to process the XML. In order to understand Microsoft's "Atlas" which provides an easier way to build these "Ajax" style applications in ASP.NET 2.0, it's useful to have a strong understanding of ASP.NET 2.0 Callback.
This article will consider an example wherein you retrieve the details of an employee based on the employee number entered in the Web page. To accomplish this, you will leverage the script callback feature and demonstrate how to retrieve the employee details from the AdventureWorks database (that ships with SQL Server 2005) without posting the page back to the server.
Before looking at the example, it is time to examine the steps involved in utilizing the callback feature.
- The client invokes a client-side method that will use the callback manager.
- The callback manager creates the request to a .aspx page on the server.
- The ASP.NET runtime on the server receives the request, processes the request by invoking a predefined sever-side function named
RaiseCallbackEventand passing in the client side argument to it. The
RaiseCallbackEventmethod stores the client-side arguments in a private variable for later use. After that, the ASP.NET invokes another method named
GetCallbackResult(), which is responsible for returning the output of the server-side call to the client-side callback manager.
- The callback manager receives the server response and invokes a callback method located on the client-side. If there is an error during the server-side processing, the callback manager invokes a separate callback method.
- The client callback method processes the results of the server-side call.
Now that you have understood the steps, Listing 1 shows an example page that implements callback feature.
Listing 1: Retrieving XML Data Dynamically Using ASP.NET 2.0 Script Callback
To understand the code better, consider the code listing as being made up of three different parts.
- Implementing the server-side event for callback
- Generating the client-side script for callback
- Implementing client callback method
Start by looking at the server-side event for callback.
Page 1 of 2