In the default model for ASP.NET Web pages, the user interacts with a page and clicks a button or performs some other action that results in a postback. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser. However, in some situations, it is useful to run server code from the client without performing a postback. If the client script in the page is maintaining some state information (for example, local variable values), posting the page and getting a new copy of it destroys that state. Additionally, page postbacks introduce processing overhead that can decrease performance and force the user to wait for the page to be processed and re-created.
To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.
Several Web server controls can use client callbacks. For example, the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.aspx control uses client callbacks to implement its populate-on-demand functionality. For more information see http://msdn.microsoft.com/en-us/library/e8z5184w.aspx.
There are several options for automating client callbacks in an ASP.NET Web page. AJAX features in ASP.NET such as the http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx server control can automate asynchronous partial-page updates for you, and the Web service communication feature can automate asynchronous Web service calls.
Creating an ASP.NET page that programmatically implements client callbacks is similar to creating any ASP.NET page, with a few these differences. The page's server code must perform the following tasks:
Implement the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.aspx interface. You can add this interface declaration to any ASP.NET Web page.
Provide an implementation for the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.raisecallbackevent.aspx method. This method will be invoked to perform the callback on the server.
Provide an implementation for the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.getcallbackresult.aspx method. This method will return the callback result to the client.
In addition, the page must contain three client script functions that perform the following actions:
One function calls a helper method that performs the actual request to the server. In this function, you can perform custom logic to prepare the event arguments first. You can send a string as a parameter to the server-side callback event handler.
Another function receives (is called by) the result from the server code that processed the callback event, accepting a string that represents the results. This is referred to as the client callback function.
A third function is the helper function that performs the actual request to the server. This function is generated automatically by ASP.NET when you generate a reference to this function by using the http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getcallbackeventreference.aspx method in server code.
Both client callbacks and postbacks are requests for the originating page. Therefore, client callbacks and postbacks are recorded in Web server logs as a page request.
In a client callback, a client script function sends a request to the ASP.NET Web page, which then runs an abbreviated version of its normal life cycle to process the callback. To ensure that callback events originate from the expected user interface (UI), you can validate callbacks. In callback validation, you register an event for validation during the Web page rendering and then validate the event during the callback. For an overview of callbacks, see http://msdn.microsoft.com/en-us/library/ms178208.aspx
Its important to note that Client Callback
, which, as far as I know, only works in IE. Hopefully workarounds and third party solutions aren't far away, which'll make this a truly powerful weapon to have in our arsenal.