public string GetCallbackScript(IButtonControl buttonControl, string argument) {
// Prepare the input for the server code string arg = “ControlArgument”;
string js = String.Format(“javascript:{0};{1};{2}; return false;”, “__theFormPostData = ‘’”,
“WebForm_InitCallback()”,
Page.ClientScript.GetCallbackEventReference( this, arg, “MyControl_Callback”, “null”)); return js;
}
How It Works
To illustrate a complete server control design is an advanced scenario and is beyond the scope and inten- tion of this book; however, an explanation of this implementation is warranted — in particular, the for- matting of the JavaScript method. The following code:
javascript:{0};{1};{2}; return false;
effectively executes the JavaScript code that will be placed in the placeholder positions {0}, {1},and
{2}, until finally returning a falsevalue. The falsevalue being returned ensures that no further JavaScript processing takes place as a result of the event raised. For each placeholder element, a section of JavaScript is embedded.
First, the {0}placeholder represents the:
“__theFormPostData = ‘’”
JavaScript code, which simply resets a hidden variable to an empty value.
155
The {1} placeholder represents the:
“WebForm_InitCallback()”
JavaScript code, which collects and prepares the form data to post to the server for the callback to the server to be correctly interpreted and trapped by the ASP.NET runtime engine. This is crucial so that the correct server-side methods are executed in response to the callback event as expected.
Finally, the {2}placeholder represents the code:
Page.ClientScript.GetCallbackEventReference(
this, arg, “MyControl_Callback”, “null”));
This code should look familiar, and is the same method used to obtain a callback event reference in the initial page-centric examples detailed previously in the discussion of the ICallbackEventHandler
interface. It is used for exactly the same purpose here, as the final step to initiate the asynchronous call- back request to the server.
To summarize, the implementation of the ICallbackContainermethod first constructs the arguments to send to the server-side callback request and returns a JavaScript block that first clears the posted data, initializes the collection of the posted data in the HTTP request (via WebForm_InitCallback()), and finally initiates the callback request by obtaining the callback event through the
Page.GetCallbackEventReferencemethod.
This section is not intended as a complete discussion on the specifics of implementing asynchronous callback functionality within your custom controls, but merely serves as an introduction on how to begin such a task with asynchronous callbacks in mind. The reader is encouraged to perform further investiga- tion in this advanced topic area and in particular in the creation of custom controls.
Summar y
This chapter has introduced the concept of Asynchronous Callback Client Scripts that are provided with ASP.NET 2.0. This feature allows a developer to utilize Ajax-like functionality within ASP.NET in a num- ber of ways.
You looked at how you could include asynchronous callbacks in your applications by:
❑ Using “out-of-the-box” server controls that come included with ASP.NET
❑ Implementing the requisite interfaces to enable your pages to support asynchronous behavior using callbacks
❑ Working with advanced techniques to develop controls that support asynchronous behavior using client callbacks
By far the easiest way to do this is to use the existing controls shipped with ASP.NET 2.0 that support this functionality, such as the GridView, DetailsView,and TreeViewcontrols. No JavaScript or
156
explicit server-side coding is required to utilize asynchronous callbacks. Simply set some properties and let the controls do the rest.
For any custom behavior, the most common application will be implementing the ICallbackEvent Handlerinterface and crafting it to meet your applications requirements. In the examples shown in this chapter, you explored various ways to interact with the client-side code and server-side code, and in particular, examined ways of packaging custom data to transfer between the client and server side.
Finally, you engaged in a brief examination of the ICallbackContainerinterface that is used for more advanced scenarios in the creation of custom controls that support asynchronous client script callback functionality.
Asynchronous Client Script Callbacks provide a framework to utilize Ajax-like functionality that is inte- grated with the server-side-centric development nature of ASP.NET. It is a powerful framework that is flexible, but does require some manual effort to customize to meet your applications requirements. Experimentation is the key to becoming adept at making this powerful feature set work the way you want it to.
It is worth mentioning here that the future of asynchronous client script callbacks actually lies in a tech- nology that Microsoft is currently developing, code named Atlas. Atlas will make the implementation of asynchronous functionality on the client as well as the server significantly easier. It will consist of a vastly enhanced client-side framework, as well as tightly integrated server controls to make what has been demonstrated in this chapter achievable with far less effort and complexity.
Atlas technology will be covered in detail later in this book starting at Chapter 10.