Yes, I LIKE WebForms!

At some of my speaking engagements recently I've made the astonishing claim that I quite like ASP.Net WebForms. Why do I say this is an astonishing claim? Quite often when I'm talking to other ASP.Net developers and we end up on the topic of WebForms you can see a look of distaste in their eyes, or there'll be a statement like "I'm stuck working with WebForms".

But when you ask someone why they don't like WebForms they generally don't have a really good reason, they come up with a few points like:

  • ViewState is bloated
  • Controls are heavy
  • It's not testable

So I thought I'd share a few of my thoughts on the topic because well, everyone wants to hear my opinion :P.

ViewState

ViewState is a double-edged sward and if you're not familiar with it and what it's goals are then you're probably going to end up doing it wrong.

First thing everyone should do when starting ASP.Net development is read this article by David Reed. The article may be 4 years old but everything still holds true today.

And once you understand ViewState you can understand how to use it to your advantage. Keep in mind that ViewState can be turned on or off for any particular control (with .NET 4.0 the control is even better), and you really should be setting it properly.

To enable or to disable?

When you turn ViewState on you're adding weight to the response back to the client (well unless you use a different provider), and this is something that you need to be aware of. Take a look at the controls you're using, what's the data they have in them and what's the cost of that data?

Say you have a literal, or a label and you're setting some text on it from a resource file. It's not that expensive to do the text setting, so why have the framework do it for you at the cost to the end user?

This principle can be applied to any kind of control, and once you start looking at what you're putting into your page you'll realise just how often you don't need to have ViewState enabled.

A little bit of planning and you'll not have to look at the giant ViewState slab.

Controls

Controls are great, they package up some functionality and make it easy to redistribute. But people often say that this is one of the big downsides of WebForms and MVC gives you much better flexibility. But think about some of the trivial (read: boring) tasks which we have to do as developers:

  • Create a login form
  • Output a collection of data using a template

So with MVC this is something that you end up having to write yourself, sure there are some helpers like Html.EditorFor and stuff so you can quickly display something. And it's true there's plenty of good extensions to do things like Repeaters, so this is just taking WebForms concept into MVC right?

One of the other main criticisms of controls is that they generate HTML for you that is hard to style, and often unchangable. But think about what they are trying to generate, a standard design cross-browser. Try having a floating layout which can be dropped anywhere and look the same?

True it makes them less flexible, but it depends what you're trying to achieve.

Testability

I've done plenty of articles in the past about testibility so I'm not going to dwell too much. All I'm going to say is that you need WebForms MVP, it's fantastic!

Conclusion

I think that WebForms is a great framework and one that we'll have with us for a long time still. If you understand what you're working with, that it's not MVC and there is a lot of power which it has to give you'll learn that it isn't really that bad :).

webforms-mvpwebformswebaspnet
Posted by: Aaron Powell
Last revised: 08 Apr, 2011 05:01 AM History

Trackbacks

Comments

10 Aug, 2010 10:52 PM

I too love webforms :)

06 Dec, 2010 12:53 AM

Well said Aaron. I keep getting more impressed about you the more I read your blog or Readify emails.

I also think the strongest feature of webforms is the control model. This is real big. Maybe thing change with compiled Razor views or something new that comes around but the reality that controls can do stuff and call things is good (as long as you understand this and you treat them as "services" or "tiny modules" or whatever not just UI widgets).
I may even go further and say the event model is just easy. Sometimes it'll feel dirty to say so, but, really, it just works! Yeah, in some advanced and really dynamic scenarios it can be painful, but once you know how to hook it up it usually becomes fun.

The real bad part about webforms to me is the parent form. This makes jQuery stuff that depends on forms sometimes weird. It might be one reason many webforms developers are not sold on client components development. When using UpdatePanel or so, the performance bottleneck in many times is not the ViewState but the size of the request with all input fields on the page uploaded to the server. It's very interesting as most people would just assume without measuring that ViewState being downloaded from the server is the reason.

Ian Patrick Hughes
Ian Patrick Hughes
25 Apr, 2011 05:33 PM

Very well put.

The near instantaneous religious fervor between the two model when MVC was first gaining momentum was a little surprising. They're two different tools, and WebForms is still very powerful and can be the best tool for a lot of application scenarios.

I started developing applications with VBScript in Classic ASP, so for me MVC still makes more sense for the overall mental model of web development. However, after 7 years of writing with WebForms, I am actually thankful for being forced to think a little differently.

I think that those who are capable of embracing both approaches will be in high demand for a long time to come.

PS - Great job on FunnelWeb. I'm in the middle of adapting it for my own blog. I'll give a shout out when it's complete.

blog comments powered by Disqus