views:

91

answers:

5

Hi,

If I am developing an asp.Net application, can there be some cases where I should forget about MS Ajax UpdatePanels and prefer jQuery.Ajax to update some part of my page?

Thank you

+1  A: 

If you are comfortable with the whole ASP.Net postback scheme, one advantage of sticking to such controls is that (most of the time) they shield you from side effecs from postbacks and such. But in this specific case it comes with a cost: you have a full postback for each AJAX request (which will include view state), which can be a performance hit compared to a straight AJAX request, cf:

http://geekswithblogs.net/dlussier/archive/2007/09/06/115188.aspx

I am also not sure on whether it handles well cross-site requests (JSONP and such), so these are two caveats to consider.

chester
+2  A: 

Sure, IMHO you should prefer jQuery over the standard ASP.Net update panels. the reason (main) is performance, when you post some data using jQuery.ajax you only post what the server needs to understand the post. this is usually 1-2 parameters of text.

when you do the same post using an update panel, the entire page goes for a walk behind the scenes to the server, then the entire page returns and the area is rendered.

if you use jQuery, you can create an ashx page on the server, this one doesn't have the Page load and lifecycle of a regular webform and it is also very lightweight.

Avi Tzurel
I have one question in mah mind... What if javascript is disabled in client browser ?
Amit
@Amit - Then it'll postback normally, you can get behavior like this with either UpdatePanels or jQuery :)
Nick Craver
hmmm okk Thanks Avi.
Amit
+5  A: 

Yes, in fact, you should almost always prefer using your own (or jQuery's) ajax functionality before.

There is a lot of overhead associated with the MS Ajax UpdatePanel (it performs a full postback, and then updates the element(s) that changed on the page), so the nice features in an AJAX enabled web site - responsivenes, continuity etc - are almost entirely lost (IMHO). You have very little control of what is actually submitted over the cable, of what is returned, and of how it is treated upon return to the client.

With jQuery Ajax on the other hand, you get full and instant control, you can make as light-weight (or heavy) requests as you wish, and lets face it - the API isn't in any way harder to use than that of the UpdatePanel.

With that in mind, there are still scenarios when the UpdatePanel is fine, or even better. Especially in ASP.NET WebForms, it can be tricky to return just part of a page in a way that gracefully degrades if the user can't use javascript, and for the rapid "drag-and-drop" development style, there is really no way jQuery can compete. (Whether you like drag-and-drop development or not is an entirely other discussion...)

Tomas Lycken
While I completely agree with the sentiment and prefer jQuery as well, the statement "just short of impossible to have more than one UpdatePanel in a page" is **completely** wrong, this is very easy to do, and common practice.
Nick Craver
Seconded! I've worked on projects where the decision was made to use UpdatePanel rather than normal AJAX and performance tends to be atrocious. In one particular case it was the combination of a DataGridView + UpdatePanel that pretty well brought the app to its knees and rendered it mostly useless. We ended up redesigning this piece of the app to avoid using an UpdatePanel.I would go so far as to say there is basically no good scenario for using UpdatePanel over any other way to retrieve your data via AJAX.
chsh
@Nick - then the problem with multiple update panels might have been solved since I last tried. As I said, this was a couple of years ago. (I was completely hooked as soon as I saw the MVC framework... :P)
Tomas Lycken
@Tomas - It's been possible and done the exact same way since they were available, so that statement was *never* true. It's an invalid statement, regardless of time context. You should remove it, that was you not using them correctly, not an inherent inability of the technology. To be clear *I despise UpdatePanels*, but to give flat out wrong information about *any* platform isn't a good way to make an argument.
Nick Craver
@Nick: There - removed it. But I am sure that when I was doing this (must have been fall of 2007), I looked everywhere I could on the internet for a solution to use several update panels in one page, and whatever I tried the thing I wanted to do (that would have been easy as apple pie with jQuery) was if not entirely, then darn close to impossible. It might have been some weird edge case, but it was the update panel, and there was no one that knew how to do it then.
Tomas Lycken
+1  A: 

I would say that you should definitely favor jQuery AJAX over the ASP.NET AJAX UpdatePanel. If you look at the ASP.NET AJAX site, there really isn't a mention of ASP.NET AJAX and/or the UpdatePanel...but there is a lot of mention of jQuery. In fact, it looks like Microsoft is favoring ASP.NET developers using jQuery as the client-side JavaScript AJAX framework over UpdatePanel and the related ASP.NET AJAX controls.

They still provide links to the AJAX Control Toolkit, but with so many jQuery plug-ins out there, why would you need the Toolkit?

UpdatePanel is slower and bulkier -- go with jQuery AJAX.

David Hoerster
A: 

As others have stated, MS-Ajax and UpdatePanels perform poorly - sometimes to the point of being unusable. As others have pointed out, the ajax callbacks which should be small & lightweight, are heavy because they also include the full ViewState.

In addition to those issues, I found some other drawbacks to MS-Ajax:

  1. The MS-generated javascript includes a LOT of overhead. They've built in a lot of type-checking and variable-checking, with the intention of making their javascript more typesafe, I assume. This overhead slows down the browser-side processing significantly - especially when those generated javascript functions are being called repeatedly/recursively (as is the case with some 3rd-party controls).
  2. The ASPX code-behind becomes much more complex and harder to maintain, because the same page lifecycle is being executed for the initial loading of the page, as well as every single ajax callback. Like all complex code, this can be overcome by skilled developers and good code comments - but it's nevertheless a drawback to consider.

A final note: the one benefit to MS-Ajax & UpdatePanels is, you can get away with being 90% javascript-ignorant and still code them. "Traditional" ajax involves FAR more javascript coding than MS-Ajax.

mikemanne