ASP.NET 4.0 & Why it Matters
I hear a lot of overused and overloaded terms these days like “leaky abstraction” when talking about WebForms. As people repeat these items like robot drones, I wonder if they truly understand what it means, and more importantly how it affects a developers ability to build software. But whether it’s a group of robot drones, or an increasing number of well educated software engineers, we’ll leave for the subject of another debate. Back to the matter at hand. Of all of the WebForms complaints, it usually boils down to a few key issues – ViewState, ID generation and HTML Markup & Postbacks; each of which is undergoing changes in ASP.NET 4.0.
ViewState
To understand the first issue of ViewState, you have to understand the ViewState model. It works recursively in nature, with each control responsible for initiating the loading and saving of ViewState from it’s child controls. The problem comes when you turn off ViewState at a root level. Since the parent control is no longer storing ViewState, it’s child controls get ignored. Basically, you can turn ViewState off selectively for children, but you can’t turn it on selectively if it’s disabled for the parent control. The solution? ASP.NET 4.0 introduces the ViewStateMode pattern which is separate from the EnableViewState property. ViewStateMode has 3 values, Enabled, Disabled and Inherit (the default). When ViewStateMode is set to disabled, any child control that is set to Disabled or Inherit will have it’s ViewState turned off. If a parent control has ViewStateMode=Disabled, and the child control has ViewStateMode=Enabled (the important new scenario), the child control will still have it’s ViewState saved. This in essence, is what the feature is all about. The net gain for a Web developer is a lighter weight page, because you now have the ability to turn off ViewState in places that you didn’t really need it on in the first place.
ID generation
Have you ever taken a look at the html markup produced by an ASP.NET WebForms application? I have. Actually, I spend a good portion of my time examining html markup and discerning how a page can be made lighter or faster based on what I see. One of the big offenders happens to be ID strings. If you want to address an element, you need to give it an ID. If you want to address it uniquely, you need to come up with a mechanism to make that ID unique. In ASP.NET WebForms this was done by pre-pending the parent control’s id to the ID string of a control. The problem is that as applications became more complex, so did the control hierarchies, which then caused UniqueID and ClientID strings to become unmanageable. Looking at an application today, it’s not out of the question to see something like “contentPlaceHolder1:ctl0:ctl0:UpdatePanel1:Panel1:GridView1” Now if you use that string a few times to address id’s or create css class names, you’re bloating your HTML. Even worse, you’re creating a recipe for cascading failures if you ever change the containership or the ID of one of the parent controls. Suddenly, your element’s ClientID has changed, and you need to go through and update your code.
In ASP.NET 4.0, you’ll have the ability to set the ClientIdMode to “Static” which solves most of these problems. With a Static ClientIdMode, the ID string you set for the control, is the same string that will be used for the ClientID. This has the benefit of being less fragile, and gives you the ability to clean up your markup by shortening ID’s considerably. The drawback to using ClientIdMode=Static is that ID’s will not be ensured to be Unique by the framework, making this a manual task for the developer. Not sure about you, but if that’s the biggest challenge of my day, I’ll be smiling.
HTML Markup & Postbacks
Postbacks were the bread and butter of ASP.NET 1.0. Luckily ASP.NET has come a long way since then, and developers today understand that creating a good User Experience means limiting postbacks. The amount of HTML rendered to the client also has a direct correlation on User Experience. The more HTML/Markup, the longer a page will take to load. ASP.NET 4.0 improves this key scenario by adding Client-Side DataBinding and templating. Think of client-side templates as a repeater that get’s populated by JavaScript. Why populate the repeater on the client-side rather than the server-side? It’s a matter of multiplication. Take the same 4 lines of HTML in a repeater template and multiply it by the number of items in your datasource. Now push all of that HTML down over the wire to the client. Make matters even more interesting, use the same datasource to populate a grid. In a typical scenario, you’re pushing the same data down two times, once for the repeater, once for the grid. If you had that data available as a client-side DataSource, you only send the data down once, and re-use it on multiple controls. At the price of a little extra processing power to create the dom elements through JavaScript, you get the benefit of possibly drastically reduced HTML markup, and free yourself from relying on Postbacks in order to populate a list.
The three features listed above are just a short list taken from the ASP.NET 4.0 features whitepaper. Even in this short list, it’s easy to see that ASP.NET 4.0 is changing the face of WebForms. Many of the concerns that developers have expressed and many of the advantages that MVC touts over WebForms are being addressed. The point is, WebForms isn’t dead, it’s quite the opposite.