Beware of Client Callbacks in ASP.NET

Feb 11, 2007

The built-in AJAX feature of ASP.NET 2.0, also known as Client Callback, is simple to utilize but a beauty in disguise. I’m not talking about the “Atlas” toolkit for ASP.NET but the ICallbackEventHandler interface that allows you to make asynchronous requests to server-side methods. It starts out being efficient and easy to use, but in the end of the website project, it will be slow and inflexible.

HTTP requests

If you trace the HTTP traffic of a Client Callback request, you will find that it sends way more than you need. I have a form that sends an asynchronous request whenever you check or uncheck a checkbox control. I would expect it to send the ID of the control and the value I give it, but actually it sends much, much more than that. To begin with, it sends the entire ViewState which could be huge. It also sends the value of many more controls than just the one I need. For the checkbox control that amounts to 10k.

POST vs. GET

It’s not a problem that it sends all the data, but it is impossible to turn it off and that’s a problem. Not only that, but the Client Callback feature only uses HTTP POST and that is slower than GET. There is no real practical reason to send POST requests – not for the developer’s side anyway – and it adds to the overhead.

All this simplicity (read overhead) is not worth it when you think of the alternatives. You could use Atlas instead or doing it manually with JavaScript and an HttpHandler. I prefer to do it manually or by a JavaScript framework like Prototype. In the case of the JavaScript framework, you still need the HttpHandler but the flexibility and performance is significantly better.

ActiveX vs. native

Another thing about the asynchronous request is that it does check for the ActiveX component before the native XmlHttpReqests object of the browser. That makes it slower in any other browser than IE6. If you do it manually, you can do it the other way around and gain performance that way as well. At the end of the day, there is not much fact talking in the favor the the Client Callback feature other than simplicity.

My experiences with the ICallbackEventHandler interface have made me turn to the alternatives. Even though ASP.NET is so easy in a lot of ways it is still important to know when things get too easy like in the case of Client Callbacks.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (7) -

Simone Busoli
Simone Busoli
2/11/2007 9:34:35 PM #

The one and biggest advantage is that the Client Callback mechanism allows you to restore the state of the page on the server-side, and thus access the values of other controls, something that cannot be obtained without sending a post request.

Mads Kristensen
Mads Kristensen
2/12/2007 6:08:17 AM #

Yes, and that is very clever, but if you don't need it there is no way to turn it off. So you are stuck with an unreasonable amount of data to send back to the server.

Simone Busoli
Simone Busoli
2/12/2007 8:16:06 AM #

That's why you have to know what you are doing and if you don't need it rely on one of many other AJAX frameworks out there like Ajax.NET.

 Manu Temmerman-Uyttenbroeck
Manu Temmerman-Uyttenbroeck
2/12/2007 5:26:34 PM #

I'm using Anthem already more then 1 year now. It's great in its simplicity. Drawback is also that viewstate is sent back and forth, but I'm using SessionViewState, so it's not really an issue. Did you ever had a look at Anthem Mads?

Mads Kristensen
Mads Kristensen
2/12/2007 9:06:58 PM #

No, I haven't tried Anthem even though it has been recommended multiple times. I should give it a go soon.

Marc
Marc
2/14/2007 4:27:00 PM #

I'd like AjaxPro (Ajax.NET) because it is very easy to learn and to use, no code change on server-side backend. If you want to use something like an UpdatePanel you are fine with ASP.NET AJAX or Anthem. I don't like UpdatePanels because of the problem that everything must go over the line. Most of the work done with UpdatePanels are more powerful used with AjaxPro, i.e. if you have something like a tab you don't need to get the data on every switch. If you got it once you can store it in JavaScript.

Dan
Dan
2/19/2007 6:46:44 PM #

Good post, I love your blog because you apply the common sense to your decisions. HttpHandler + Javascript is the way to go if you are not interested in using the viewstate features in your asynchronous call.
Anthem does the same thing that asp.net client callbacks, uses regular expressions to restore viewstate, event validation... too much overhead. Anyway, Anthem is the best option to bring the ajax world to the classic asp.net paradigm. But the question is: the asp.net page model ( viewstate, postback validation, POST the entire page with javascript... ) is the best option under the asp.net architecture ?
I'm sure you'll write about this not very late


Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.