Introduction

Since ASP.NET 2.0, the .NET framework ships a feature called client callback. These callbacks are much lighter than the partial postbacks provided by the ASP.NET AJAX Extensions' UpdatePanel. Client callbacks send a specified string to the server, and retrieve only a server side generated string as result rather than a re-rendered partial page, as in the case of the UpdatePanel.

Nasir Ali Khan has done an exellent job in his article on CodeProject about client callbacks.

This article consists of the following three parts:

Multiple client callback implementation.

Simple ASP.NET page example.

Custom composite control example.

Prerequisite

The reader of this article needs to be familiar with the concept of client callbacks being provided as part of the .NET framework since version 2.0 .

Part I: Multiple client callback implementation

The implementation of the ICallbackEventHandler interface restricts a page/control to a single callback method declaration (RaiseCallbackEvent(string eventArgument)).

What others suggested:

In order to distinguish callbacks in the RaiseCallbackEvent method, prefixing the eventArgument string was suggested in other articles. The author's opinion is that this is an unaesthetic and error prone approach.

Smarter approach:

A custom control shall serve as a "shell" for the sole purpose of hosting the client callback functionality:

The control implements the ICallbackEventHandler interface, and exposes the callback method as a public event (Raise).

By adding as many ClientCallback controls to a page or user/custom control as the required callbacks, the implementation for each of them can by nicely kept apart.

Part II: Simple ASP.NET page example

In the Visual Studio Designer, the ClientCallback control can be easily placed onto the ASP.NET page by drag and drop. The following code example contains two ClientCallback controls, and two Buttons that initiate the callbacks. For the simplicity of this example, both Buttons use the same callback complete function. However, in a real world implementation, you would implement a function for each of them in order to handle the result accordingly.

The ClientCallback control events get hooked up in the Page_Init method. Of important notice is that the callback event references need to be assigned to the appropriate ClientCallback control.

The buttons in this example send an empty string as an argument, and the retrieved result is just a simple text too. However, if you think of exchanging JSON or XML, these client callbacks might become very powerful!

Part III: Custom composite control example

Having client callbacks nicely structured comes in even more handy when it comes to custom web control development. The following composite control implements the same functionality as the page in part II.

Comments and Discussions

hi,
If I put a break point against page_load then everytime I hit either button the page_load event fires.
Is that expected behaviour? does call back invoke page load?
I thought I understood callbacks but this seems to clash with my understanding