Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 **

like image 426
Nick Avatar asked Aug 25 '09 19:08

Nick


People also ask

What is the difference between callback and postback in asp net?

"A callback is generally a call for execution of a function after another function has completed." But if we try to differentiate it from a postback then we can say that it is a call made to the server to receive specific data instead of an entire page refresh like a postback.

What is callback function in asp net?

In a client callback, a client script function sends a request to the ASP.NET Web page, which then runs an abbreviated version of its normal life cycle to process the callback. To ensure that callback events originate from the expected user interface (UI), you can validate callbacks.

Why we use AJAX call in MVC?

It is a client-side script that communicates to and from a server/database without the need for a postback or a complete page refresh. The Ajax speeds up response time. In other words, Ajax is the method of exchanging data with a server, and updating parts of a web page, without reloading the entire page.

How can make AJAX call in ASP NET MVC?

Open your Visual Studio and create a empty ASP.NET MVC application. Click on File -> New Project -> Web -> ASP.NET web application. From the next window Select template Empty and from Add folders and core reference choose MVC. Name it as AJAXCalls and click Ok.


2 Answers

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.

like image 162
Jeff Sternal Avatar answered Oct 26 '22 11:10

Jeff Sternal


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.-

like image 28
Sebastian Avatar answered Oct 26 '22 11:10

Sebastian