Tutorial :Difference between Client Callbacks and Ajax Page Methods - ASP.NET


Based on my understanding, both of them essentially do the same thing (lets us execute a server side method from JS). Are there any differences?

Also, Ajax Page Methods can be implemented either using JQuery or using ScriptManager. Which one is preferred and why??

**BOUNTY: Adding a bounty to get clear explanation of the question. Thanks **


Fundamentally, Client Callbacks and Ajax Page Methods are doing the same thing. They use an XMLHttpRequest object to send a request (usually asynchronous) to some URL, get the results of that request, then execute a callback method you've provided (callback with a lowercase c), passing the results of the request to your method.

Having said that, there is one big difference between the two approaches:

  • Page Methods are implemented as static methods on your page. Your page class is just a convenient container for these methods, which could really be hosted anywhere (a web service, a custom HttpHandler, etc.). Since no instance is ever going to be constructed, the client doesn't have to send ViewState data and Asp.Net doesn't have to run through the Page's lifecycle. The flip side is that you don't have access to to your Page class's instance methods and properties. However, in many cases you can work around this by refactoring instance methods into static methods. (See this article for more information.)

  • Client Callbacks are implemented as instance methods on your page. They have access to other instance methods on your page, including stuff stored in ViewState. This is convenient, but comes at a price: in order to build the Page instance, the client has to send a relatively large amount of data to the server and has to run through a fair chunk of the page lifecycle. (This article has a nice diagram showing which parts.)

Apart from that, the cost of setting them up varies quite a bit and clients use them differently:

  • Client Callbacks require a fair amount of idiosyncratic scaffolding code that is intimately coupled to Asp.Net (as shown in the link above). Given the much easier alternatives we have now, I'm tempted to say that this technology is obsolete (for new development).

  • Calling page methods using ScriptManager requires less setup than Client Callbacks: you just have to pop a ScriptManager onto your page, set EnablePageMethods = true, then access your page methods through the proxy the PageMethods proxy.

  • Calling page methods using jQuery only requires you to link the jQuery library (and familiarity with jQuery, of course).

I prefer to use jQuery to access page methods because it's independent of the server framework and exposes just the right amount of implementation details, but it's really just a matter of taste. If you go with ScriptManager, its proxy makes the page method calls a little easier on the eyes, which some might consider more important.


I would say there are differences, but would tend to say do it the way you feel more comfortable with.

I have used both approaches, and having jQuery calls from the page is generally faster. I write an ashx handler that does the job the jquery call needs (query the database, process something, etc.) and call it from the page. I wouldn't use an aspx page for a jQuery call, because you're sending a lot of info that you won't need at all. The difference/ benefit of using an Ajax.Net call is that you don't need to build another page to process things, you can use the same page events to do it.

For example, if you need to fill a second drop down list using the selected value on a first one, you could use Ajax.Net to call the SelectedIndexChanged in the page code behind and when it fires go Page_Load, SelectedIndexChanged, Page_PreRender and so on. In the event method you'd query the db and fill the second ddl.

With jQuery that could be a bit different. You make your call to an ashx handler, the handler is just a server method that do the magic and return data in the form you want to have (json, array of strings, xml, etc) and fill the second ddl using javascript. As I told you before, some people doesn't feel too mcuh comfortable with Client code and tend to do it in the server, but I always say that you need to use the correct tool for the right job, so know your tools and apply them wisely.

If you want to know more about ASP.Net, ASHX handlers and jQuery, you can read a post that I wrote about it.

Hope it helps.-


They are essentially the same. Both:

  1. Setup a webservice for you that the javascript for the control can call.
  2. Provide asynchronous response without involving the page lifecycle.

They are different:

  1. Page Methods simply require that you decorate a static method with an attribute and you are done. The rest of the magic is handled by HTTP Handlers and Modules. Callbacks require you implement a few interfaces and handle the async event handlers yourself. I find them to be a little more of a pain.
  2. Callbacks only work with certain controls. Calling page methods allows you to affect any control through custom javascript. Callbacks have a slight advantage here in that the client-side behavior is already written and fixed. With page methods you have more flexibility, though (the behavior on the client side is determined by you).

There are a few other differences, but these are the basics. My understanding is that client callbacks tend to perform as well as Page methods, but are not used as much becuase they are only available in certain situations, whereas a Page Method is always a valid avenue.

As for the ScriptManager vs. JQuery question, my feeling here is it's about taste more than anything. I like JQuery's syntax and I feel like it performs better, but in the grand scheme of things the most expensive thing is the XmlHttpRequest... after that the execution of the javascript is likely to be insignificant in difference next to that.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »