开发者

Is there a reason to prefer jQuery.Ajax to asp.net UpdatePanel?

If I am developing an asp.Net application, c开发者_如何学Goan 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


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


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜