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
XMLHttpRequestobject 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 sendViewStatedata and Asp.Net doesn’t have to run through thePage‘s lifecycle. The flip side is that you don’t have access to to yourPageclass’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 thePageinstance, 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
ScriptManageronto yourpage, set
EnablePageMethods = true,then access your page methods through the proxy the
PageMethodsproxy.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.