Browse DevX
Sign up for e-mail newsletters from DevX


Whidbey Simplifies Browser Client Script Callbacks : Page 4

In Whidbey, there's a better way to call remote server methods without using postbacks or remote scripting—letting you update information in the browser without redrawing the entire page. Find out how to fetch data from the server using a client call.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Handling Callbacks
Now you need to add two client methods to handle callbacks: a CallBackHandler method to handle successful callbacks, and an ErrorCallBack method to handle any errors. Listing 4 shows the script definitions. Note that both methods have similar signatures. The CallBackHandler method's result parameter will be the list of delimited strings returned from the RaiseCallbackEvent, and the value of the context parameter will be the appropriate control id (the same as the CallServer method's second parameter—remember, you're passed the "id" of the country control as a context parameter in Listing 2). Note that the if(context == "cboCountry")... statement uses the context to get the reference to the appropriate form element. With that information in hand, all that remains is to handle the return value, using split to obtain an array with which you populate the appropriate dropdown list.

Error Handling
Whenever a server-side error occurs during remote calls the CallBackManager routes the callback to the ErrorCallBack method. Listing 1 shows how it assigns the ErrorCallBack. The ErrorCallBack method included in the sample code just displays a client-side alert message, but you should include more appropriate error-handling in deployed applications. The result parameter value provides the actual error message. To test this, throw some exception from the server-side event, and then check the value. You're ready to run the sample and see the callback work. If you opt to run the enclosed sample, extract the files to a folder and open this folder as your Web site. Be sure to check and update the connectionstring value in the Web.config file before you run the project.

Author's Note: Not all browsers support the callback implementation. To find this, use the class HttpBrowserCapabilities and check the return values of the two new bool properties: SupportsCallback and SupportsXmlHttp. A return value of True means the browser supports the callback implementation.

The callback capability in ASP.NET v2.0 is a far better implementation model than remote scripting. You can implement callbacks to retrieve lookup data, invoke backend events, or validate user input values. Callbacks can improve performance by fetching only relevant data asynchronously. Using XML can make your callbacks even more powerful. You can use the support for error callbacks very effectively to validate user input. But the biggest advantage—by far—is freeing your end users from the unpleasantness of the full-page postback/redraw cycle.

Vishnu Prasad H is a Microsoft MVP working as consultant for Capco IT Services India Ltd, Bangalore. He has four years of software development experience and has expertise in developing applications using Microsoft technologies and languages. You can reach him via email here.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date