2 May
How’s that for an inflammatory title?
Let me prefix this post with a brief disclaimer. I like Microsoft’s .NET platform. C# is a great language to work in, it’s flexible & powerful. Plus, the .NET framework contains some great functionality that makes my job so much easier. In short, C# rocks.
Having said that, I have some issues with ASP.NET. Deane Barker from Gadgetopia has highlighted some of these issues, and while I’m not quite as down on it as he is, I think he makes some good points
ASP.Net is such an abstraction. It’s like one day Bill decided that we all knew too much about how the Web worked, and he needed to invent a framework that changed all the rules and hid all the scary HTTP stuff so that a generation of Web developers that he trained on his stuff exclusively would have a very skewed perspective about how the Web actually worked.
I hope that didn’t sound arrogant, but I do think that if you’ve never done any Web development except ASP.Net using Web Forms, then there’s a lot you missed about Web development.
I think the UpdatePanel released with the ASP.NET AJAX Extensions really demonstrates this issue. The UpdatePanel is really powerful. It allows you to write your code essentially the same as you always have, with a few small modifications. You then just wrap your ASP/HTML controls in an UpdatePanel and hey presto, you’ve got them running as AJAX controls. No page refreshes, no postbacks. Except that last one isn’t true, it just looks true. On each AJAX event (that is, any event that’s wired up in the code behind for a control that’s a trigger for an update panel), the page does a postback. The full page load life-cycle, including the view state. It has to be this way, or how else could you still access all the values and states of the controls in your code behind methods?
This is just the problem, it makes the development of AJAX-type sites too easy. You don’t have to know what’s going on behind the scenes, you don’t have to understand the asynchronous calls made & the HTTP processes going on.
Now, this may not be all that important if you’re on a small site, with very few steps in the page life-cycle implemented, and small view state. But if your site is large and complex, and you have a lot of controls on the page (ie large view state) this becomes a real issue. If you page currently takes a couple of seconds to load, due to complex processing and data connections, then on every AJAX call, this is how long it’s going to take. Obviously you won’t need to wait for the full page render time, but this is not usually the most time-consuming part of the page load process. Also, you should always make sure you don’t double handle stuff by wrapping as much as possible in a if (!IsPostback) block, but if the page is complex, this will still not always be enough.
Now despite the title of this post, I don’t think the UpdatePanel is universally bad, I just think it needs to be used carefully. You should be aware of what’s going on. Whenever you implement an UpdatePanel, you should always use something like FireBug to debug the asynchronous calls, to see just how big each request is. Because this will affect overall site load times and user experience (UX in marketingese). You need to be aware that in all cases, ASP.NET is breaking the HTTP process to make it stateful and allow development to happen more like Winforms.
You should also be very circumspect about where you use UpdatePanels. If you have a large form that all need AJAX type functionality (postcode lookups, inline validity checking, etc) then it may be valid to use the UpdatePanel rather than hard coding the AJAX features for each control. Just realise that you are trading performance for developer time. This is not always wrong, you just need to be aware that the UpdatePanel is very ‘chatty’ and there is probably a more efficient way of doing it if you’re having page load issues.
However there are cases where an update panel is not appropriate. Allow me to demonstrate. Lets say this is on your page
[sourcecode language='html']
[/sourcecode]
with this in your code behind
[sourcecode language='csharp']
protected void ShowHideButton1_Click(object sender, EventArgs e){
Panel1.Visible = !Panel1.Visible;
}
[/sourcecode]
If you ever find yourself writing code like this, YOU’RE DOING IT WRONG!
Just to toggle the visibility of a div, you have to send the viewstate back to the server, run through the whole page load life-cycle then get the response back from the server to the user’s browser.
Why go through all this when with one line of Javascript you could do this
[sourcecode language='html']
[/sourcecode]
Or if you wanted it to be even easier, include the Prototype library and do this
[sourcecode language='html']
[/sourcecode]
There’s no traffic between the user’s browser & the server now, there’s no waiting while the page loads on the server, it’s instant.
My point here isn’t that ASP.NET & AJAX.NET are bad. They’re not, they’re very powerful, I work with them every day. I just think that if all you know is WebForms, you’re in real danger of shooting yourself (and in turn the company you work for) in the foot. You need to be aware of what’s going on under the hood. You need to understand the HTTP life cycle, you need to be aware of what AJAX actually does
But I still think the UpdatePanel is evil…
5 Responses for "The UpdatePanel is evil"
I totally agree with you. They are great tools, but you should invest some time to understand how these tools work behind the scenes.
I’ve recently started working in C# and ASP.NET after years of using PHP, Perl, Java and Python on mostly UNIX platforms.
Honestly, I like ASP.NET a lot. The basic templating system, that lets you manipulate the ASP.NET document at the DOM level is fantastic.
The Web Controls, however, are retarded. They make ASP.NET seem like an advanced incarnation of 1990’s web construction rather than the kind of modern framework application that good programmers are building in Ruby, PHP, Java and other languages — it’s like they’re trying to compete with Cold Fusion, not the living languages that people are using now.
I ‘d love to have something that works like Web Controls, but the viewstate concept just isn’t compatible with the modern approach that people call MVC: real internet applications need the ability to display different ‘views’ based on form input and database results — to carry information through a series of related forms. Viewstate is completely incompatible with that, not to mind the browser back button. Reliable applications keep ~most~ form state in hidden variables, or implement something that has the same semantics: there’s a very limited amount of stuff that you can keep in session scope if you want to make apps that really work.
Perhaps Microsoft’s MVC framework for ASP.NET will improve the situation, or perhaps somebody will make something that looks a lot like the web controls but that actually works.
[...] ASP.net thoughts May 5, 2008 at 5:54 am | In Uncategorized | Tags: asp.net Saw this article: The UpdatePanel is evil [...]
Very good point. However, I have to disagree that UpdatePanel is evil. It just have to be used carefully, like you mentioned. The tool that can really help you with ASP.NET Ajax is Web Development Helper. It really rocks! Check the description here http://www.jankoatwarpspeed.com/post/2008/04/21…
Whenever I hear “X technology makes Y functionality too easy”, I cringe. It’s ease of use is not the problem; in fact, that is its great value. The fact that the implementation is hidden from you is the problem. In fact, that’s my problem with many MS tools/frameworks/etc.: they make it easy to get started, but they don’t give you the option to dig into the internals to solve the really tricky problems. With the UpdatePanel, I believe you can dig into the javascript files if you’re really interested, but I’d much rather have a handy debugger visualizer that showed me exactly what was being sent and received in each ASP.NET AJAX request.
And on your point of “you’re doing it wrong” by setting the visibility, your blanket statement is not accurate. If the div contains ASP.NET validation controls, you need to set the visible property on the div. If you just change the display style, you’ll end up with a validation control hidden but preventing (possibly) the page from being posted. That’s a very bad and confusing thing.
Leave a reply