Async callbacks in server controls

A discussion about the "Callbacks" feature in ASP.NET Whidbey, and a sample control and slide deck to go along with it.
Callbacks came up in a discussion I was having while brainstorming some v-next ideas. I think callbacks have the potential of enabling a whole new genre of smart Web applications. This feature happens to be a personal favorite of my mine. In one sense, callbacks are a much improved reincarnation of remote scripting, which I worked on as part of my first project upon joining Microsoft and the Visual InterDev team way back in '97.

So far in ASP.NET, postbacks have been the primary mechanism to incrementally update the UI. But this has some associated problems: back button enabling, scrolling, refreshing etc. They are appropriate when their use is in sync with the user's semantic model - for example, when submitting a form, and not the framework's semantic model. On the other hand, incremental work and UI updates within the same page rarely qualify as a scenario for postbacks. For example, when a tree view node is expanded, the end user is least expecting a new page to show up. Quite the contrary is true... he is actually expecting an interactive display where the node expands in-place without disturbing anything else on the page.

"Callbacks" is a fancy name for what is essentially an out-of-band request to the server. Rather than posting a form or a page and receiving a brand new and complete page in response, some intelligent client script can package up data for processing, ask the server to process it and then consume the result. This intelligent script can then seamlessly blend that result into the page's existing rendering. While this is happening, the page remains completely interactive. So the user experience is improved on two counts: the page remains interactive while the communication with the server happens, and second, the new result is shown without flicker, loss of current position, or spurious navigation. Things become less choppy allowing Web applications to emulate some of the rich client application behaviors more closely.

When we started implementing the TreeView control for the next version of ASP.NET, the first step was to put in-place a framework that would allow us to build a more realistic user interface model. We built this framework in a general way so other controls could use it just as well, resulting in what is known as the Callbacks feature. I have a very simple example, a PerformanceCounterLabel control that uses callbacks to continually update the display of a performance counter value without refreshing or posting the page. The control demonstrates how you implement ICallbackEventHandler to use the callback framework.

The basic idea behind callbacks isn't completely new. I think the innovation here is really about how we integrated this into the page lifecycle. This integration stems from our fundamental requirement, which is so fundamental in fact, that as far as I recall, it has remained unspoken for the most part. The requirement is to allow the page developer to remain oblivious of the callbacks. It is the responsibility of the framework and server control developers to make magic happen. First and foremost this implies that the page developer continues to write a single page and that secondly, they continue to write code in event handlers. How this was accomplished is described below.

The framework that we provide has two parts. The first is a script library that implements the logic to perform asynchronous HTTP requests back to the server, receive the response, and then invoke the script callback rendered by the control. Our implementation uses XMLHTTP, which is available in IE as well as in Mozilla. We package up the data you want to send down to the server, the target of the callback (the unique ID of the control) as well as the view state of the page (you'll see why in a moment) in the __CALLBACKPARAM, __CALLBACKID and __VIEWSTATE request variables respectively and perform an HTTP request.

On the server, the page starts executing its lifecycle, as though it were a POST request because of the view state presence. Very soon however, it also notices the __CALLBACKID variable, which not only identifies the target of the callback, but also puts the page into Callback. The page now executes a trimmed down lifecycle involving the Init, Restore ViewState and Load phases, followed by an invocation of the target control's ICallbackEventHandler implementation, and then immediately disposing the page, thereby skipping the rendering phase.

So we satisfied the first part of the fundamental requirement. The callback request is made to the same page, allowing the page developer to put all their logic into a single page, and code as if nothing has changed. This was made possible by having the page lifecycle be aware of callbacks, so it could do the right things, or rather not run through the logic that isn't meaningful. The next part of the requirement was to enable the event-based programming model. It is likely that page developer will access properties or method of server controls in their events, and that server controls themselves will want to interact with each other during the callback request as well. This is where the view state comes in. Just like in the postback world, the view state restores the page to the state it existed in during the last request, callbacks also use view state to restore the page to their previous state before the Page_Load and the ICallbackEventHandler implementation is called. This makes the event-based model work despite the stateless environment. Furthermore, this allows a server control author to also remain mostly oblivous of callbacks as well - they can just assume their control is in the right state and perform their logic. The framework takes care of the details!

Here's a slide deck that contains an animation of the request/response loop and the page lifecycle for both the postback and callback models.


I think we've just scratched the surface when it comes to actually using callbacks in Whidbey with a ton of scenarios yet to be realized... imagine asynchronous validators validating by executing server-side validation logic, background fetching of data with client-side paging, switching between multiple views of a control etc. etc. I'd love to hear what ideas people have for using callbacks to enhance their controls, as well as any comments you have on the sample.
Posted on Friday, 6/25/2004 @ 8:38 PM | #ASP.NET


Comments

16 comments have been posted.

Jay Nathan

Posted on 6/26/2004 @ 5:56 AM
Great concept, Nikhil. I'm excited to see it in motion. One question: When a callback is raised from the client and data is sent back to the server, including view state, what if the values of an input control on the client have changed? The view state would technically be out of date (according to the user's expectations). Will the values of the input controls on the page also be wrapped up in the XMLHTTP post?

Jay Nathan

Posted on 6/26/2004 @ 5:57 AM
I should add to my question above, that the user expects the updated control's value to be used in the processing of the callback event.

kiran

Posted on 6/26/2004 @ 7:21 AM
good. Will there be any support in the framework for making the calling page a model window while the callback is occurring? In the tree view control scenario user pretty much knows what happens when she clicks expand node. But in other scenarios, if a form control is causing state change in multiple other controls, user have no idea what happens.

Aaron Boodman

Posted on 6/26/2004 @ 7:59 AM
Awesome work, Nikhi. It's great to see this stuff becoming first-class in ASP.Net. It's been a lot of grungy and hard work to get these effects up until now.

George

Posted on 6/26/2004 @ 10:53 AM
You mean something like this ?

http://georgenava.com/samples/sodascript.html

Rene

Posted on 6/27/2004 @ 2:22 AM
It will give new posiilities but I see the same problems as Jay stated.
I can imagine scenario's where the complete content of the page changed but the viewstate is still the same.
It is possible to change the innerHtml of a placeholder control with new Input controls based on a callback.
Nevertheless, it is powerfull and as with everything with power: handle with care.
More important is that this added power is not an IE only party but also available for some other browsers.
Is there some 'Fallback' scenario? How will an browser not supporting XMLHTTP react?

Binesh

Posted on 6/28/2004 @ 7:33 PM
We are using XmlHttp to trigger a server side call in our current implementation (ASP.NET 1.1) and the limitation that we face is that the contents of the grid control cannot be changed. Can callbacks now be used in this kind of scenario

Moin

Posted on 10/20/2004 @ 12:21 PM
I developed a server control that tries to simulate the windows popup message behavior. I have buttons options like (YesNo, YesNoCancel, RetryIgnore,etc). One thing that I have been dying to implement is to call a public function like 'showMessage' that would show the message(my server control). When the user clicks on one of the buttons I would like the event to be captured and returned by this show function as the return value. I currently have the message come up with diferent click functions exposed which is not what I want. I tried using the delegate model to carry this out but with no luck. In the first place is it possible to do what I am trying to do? Just wondering if I can use the callbacks? Thanx.

Richard Purchas

Posted on 12/1/2004 @ 4:55 PM
I love this concept, but wonder whether callbacks to the same page is necessarily what you want to do; i.e. I'd like the callback to make an async post to a non-page URL - typically something serviced by an HTTPHandler. This approach would allow my client script to callback without viewstate, with a defined set of inputs, and without the expensive page construction/lifecycle. Ideally, a design-time set of tools would make this complete! e.g. enabling at design time, specification of the URL to call back to and also which client-side values to bundle in some XML. Of course, callbacks to the originating page would alos be desirable.

Steve

Posted on 12/29/2004 @ 10:27 AM
"and the limitation that we face is that the contents of the grid control cannot be changed"

I bind on the out of band page and then send the datagrid back as a string. I control all the columns in code.

I'd like to call a page with a datagrid on it and have that display in the div tag rather than create that datagrid in runtime

Tom Kiefer

Posted on 3/22/2005 @ 5:13 PM
Looks to be a great feature. I already do stuff like this, but the ASP.NET Callback mechanisms look to be able to package for me much of the plumbing I had been doing. Good stuff.

I just have a question to toss into the mix of info about ASP.NET 2.0 Callbacks...

Does a Callback request (which is effectively a specialized call to the original page on the server) appear in the Web server's IIS logs as a request for the page in question?

If so, this detail should be as well-documented as possible, before the log data that many depend on for basic page-view statistics starts ballooning.

Thanks!

RR

Posted on 5/12/2005 @ 9:58 PM
I The ARTICLE IS GOOD TO UNDERSTAND abt Async callbacks in server controls

Suresh Jeyakumar

Posted on 6/8/2005 @ 4:58 AM
I have been using the call backs using the Ajax.Net implementation
provided by Michael Schwarz at http://ajax.schwarz-interactive.de/.
I had the same ViewState related issues when using the dll and had to
come up with extra workarounds to get the stuff working. But mostly I
have been using Ajax to fill related dropdown boxes and so not much complications.
I will have to give credits to Michael as well for a good set of working samples.

It would be quiet interesting to see the MS Implementation. But I am little apprehensive about
the lifecycle over head instead of just a simple HTTP handler.

Rui Quintino

Posted on 6/13/2005 @ 3:51 PM
Just to drop an experimental ajax+asp.net postbacks+in place update implementation. Something like using normal postbacks, mark changed controls for rendering & update that controls only.

http://weblogs.pontonetpt.com/rquintino/posts/4831.aspx
http://rquintino.europe.webmatrixhosting.net/sample.aspx

home security

Posted on 12/13/2005 @ 9:42 AM
href="http://www.homessecuritysystems.net"> home security systems </a>
href="http://www.homessecuritysystems.net"> home security camera </a>
href="http://www.homessecuritysystems.net"> wireless home security </a>
href="http://www.homessecuritysystems.net"> home security products </a>
href="http://www.homessecuritysystems.net"> home security alarm </a>
href="http://www.homessecuritysystems.net"> home security safe </a>
href="http://www.homessecuritysystems.net"> home audio video security cameras </a>
href="http://www.homessecuritysystems.net"> home security alarm system </a>
href="http://www.homessecuritysystems.net"> home security camera system </a>
href="http://www.homessecuritysystems.net"> home security cameras </a>
href="http://www.homessecuritysystems.net"> home computer security </a>
href="http://www.homessecuritysystems.net"> cheap home security system </a>
href="http://www.homessecuritysystems.net"> home automation </a>
href="http://www.homessecuritysystems.net"> x10 home automation </a>
href="http://www.homessecuritysystems.net"> automation home wiring wireless </a>
href="http://www.homessecuritysystems.net"> home automation software </a>
href="http://www.homessecuritysystems.net"> home security device </a>
href="http://www.homessecuritysystems.net"> wireless home security camera </a>
href="http://www.homessecuritysystems.net"> home security equipment </a>
href="http://www.homessecuritysystems.net/X10/camerasystems/camera_systems.html"> home audio video security cameras </a>
href="http://www.homessecuritysystems.net/X10/camerasystems/camera_systems.html"> home security cameras </a>
href="http://www.homessecuritysystems.net/X10/camerasystems/camera_systems.html"> home security cam </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home computer security </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home security monitoring </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home security company </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> best home security system </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home security surveillance </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> free home security </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> computer home security system </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home surveillance security system </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home security products </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> san diego home security </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> wireless home security cameras </a>
href="http://www.homessecuritysystems.net/X10/homesecuritysystems/home_security_systems.html"> home provider security system </a>
href="http://www.homessecuritysystems.net/X10/software/software.html"> home security software </a>
href="http://www.homessecuritysystems.net/X10/software/software.html"> homes security software </a>
href="http://www.homessecuritysystems.net/X10/software/software.html"> home automation software </a>
href="http://www.homessecuritysystems.net/X10/entertainment/entertainment.html"> home entertainment </a>
href="http://www.homessecuritysystems.net/X10/entertainment/entertainment.html"> home entertainment systems </a>
href="http://www.homessecuritysystems.net/X10/entertainment/entertainment.html"> home entertainment center </a>
href="http://www.homessecuritysystems.net/X10/entertainment/entertainment.html"> home entertainment centers </a>
href="http://www.homessecuritysystems.net/X10/entertainment/entertainment.html"> home entertainment system </a>
<a href="http://www.missmela.com"> wedding dress </a>
<a href="http://www.missmela.com"> prom dress </a>
<a href="http://www.missmela.com"> formal dress </a>
<a href="http://www.missmela.com"> homecoming dress </a>
<a href="http://www.missmela.com"> dress up </a>
<a href="http://www.missmela.com"> evening dress </a>
<a href="http://www.missmela.com"> sexy dress </a>
<a href="http://www.missmela.com"> girl dress </a>
<a href="http://www.missmela.com"> womens dress </a>
<a href="http://www.missmela.com"> couture </a>
<a href="http://www.missmela.com"> fashion design </a>
<a href="http://www.missmela.com"> fashion dress </a>
<a href="http://www.missmela.com"> models </a>
<a href="http://www.missmela.com"> teen models </a>
<a href="http://www.missmela.com"> america next top models </a>
<a href="http://www.missmela.com"> swimsuit models </a>
<a href="http://www.missmela.com"> fashion dress </a>
<a href="http://www.missmela.com"> sexy models </a>
<a href="http://www.missmela.com"> young models </a>
<a href="http://www.missmela.com"> male models </a>
<a href="http://www.missmela.com"> bikini models </a>
<a href="http://www.missmela.com"> model based reasoning </a>
<a href="http://www.missmela.com"> womens dress </a>
<a href="http://www.missmela.com"> plus size dress </a>
<a href="http://www.missmela.com"> party dress </a>
<a href="http://www.missmela.com"> sexy prom dress </a>
<a href="http://www.missmela.com"> buy dress </a>
<a href="http://www.missmela.com"> buy flower girl dress </a>
<a href="http://www.missmela.com"> buy wedding dress </a>
<a href="http://www.missmela.com"> buy dress online </a>
<a href="http://www.missmela.com"> buy prom dress </a>
<a href="http://www.missmela.com"> sexy woman </a>
<a href="http://www.missmela.com"> best prom dress </a>
<a href="http://www.missmela.com"> cheap prom dress </a>
<a href="http://www.missmela.com"> cocktail dress </a>
<a href="http://www.missmela.com"> cheap homecoming dress </a>
<a href="http://www.missmela.com"> black dress </a>
<a href="http://www.missmela.com"> prom dress 2005 </a>
<a href="http://www.missmela.com"> cheap wedding dress </a>

Amir Leshem

Posted on 12/21/2005 @ 3:32 PM
zumiPage for ASP.NET is a better solution for AJAX-based web applications.
http://www.zumipage.com
The discussion on this post has been closed. Please use my contact form to provide comments.