Custom Search

Sunday, June 7, 2009

Asynchronous client script callbacks

ASP.NET 2.0 contains a technology called Asynchronous Client Script Callbacks, or simply callbacks for a shortened name. As the name suggests, this offers the ability for ASP.NET to directly support the inclusion of JavaScript code that enables asynchronous calls to the server for processing—that is, to execute a server method asynchronously from within the browser and have ASP.NET instruct the browser to execute a JavaScript callback method when the server method completes.

How its protential for your own application?

The answer is twofold.

❑ First, there is a newly introduced interface named the ICallbackEventHandler interface.
There is another interface called the ICallbackContainer interface that indicates that a class
can generate its own script to invoke a callback, but it is not the primary interface used when
dealing with Asynchronous Client Script Callbacks and will be detailed later in this chapter.

❑ Second, in a change from ASP.NET 1.0/1.1, all client scripting functions are now housed in a
client script object (which is of type System.Web.UI.ClientScriptManager) that exists as a
member within every ASP.NET page object. This houses the required functions to support the
client-side generation and manipulation of JavaScript code, in particular to work in conjunction
with your server-side methods to be called asynchronously.

What is ICallbackEventHandler Interface?
The ICallbackEventHandler interface is the primary mechanism for enabling pages or controls to
participate in the Asynchronous Client Script process. By implementing this interface within a control or page, server code can generate the relevant JavaScript code that allows asynchronous interaction between the client browser and the server. This interface has two methods associated with it:

Interface Method/Description
string GetCallbackResult( ); / This method returns the result of the callback event/asynchronous call to the client browser.

void RaiseCallbackEvent(string eventArgument); / The method that is called asynchronously to handle the callback event. The eventArgument parameter contains any data passed in via the callback event/method.

A web control or web page that intends to provide support for Asynchronous Client Script Callbacks needs to implement this interface and provide implementations for the two interface methods described in the preceding table.

The reason there are two methods is to provide support for asynchronous datasources, a new feature in .NET 2.0. An asynchronous datasource will typically initiate a request to get data and then, at some later stage, process the results of the initial call when they become available. Having two methods to deal with asynchronous callbacks from the client provides this server-side support for asynchronous datasources.

These events are central to all callback events. This means that if a page implements this interface in order to act as the recipient for Asynchronous Client Script Callback events, the RaiseCallbackEvent method will be invoked for all client script callbacks defined within the page, whether one or one hundred. This has implications for complex situations where you have multiple ways of triggering a callback method from the client and multiple types of data associated with each event that are passed with each call. This will become more apparent as you examine some more complex scenarios that utilize Asynchronous Client Script Callback functionality.

For more information please visit -