ASP.NET 4.0 Webforms Enhancements

Following on from my post on VS2010 Enhancements for ASP.NET I thought I’d spend a little time describing some of the new ASP.NET 4.0 Webforms features.

1 – Client IDs

Yes indeed. Now you can have your cake and eat it – almost. We give you control over the ClientIDs generated by server controls. ASP.NET does an excellent job of avoiding naming conflicts in the HTML document through the use of naming containers that guarantee uniqueness for a certain context. The result though, can be lengthy, complex and (importantly) difficult to predict ClientIDs.

This can make life a misery if you’re doing DOM manipulation in Javascript as you typically revert to some inline code to extract the ClientID. You’ll find <%= MyControl.ClientID %> littered all over the place. Given the surge in popularity of AJAX and client frameworks like jQuery, the pain caused by this “guarantee of safety” is becoming more acute.

Step forward the new ClientIDMode, an enumeration capable of taking on 4 values:

  • Legacy - Behave as ASP.NET 2.0 (this is set at the page level in machine.config)

  • Inherit (default) – Get the value from the parent

  • Static – Do exactly as I say. In other word, what you set as the ID becomes the ClientID. ASP.NET leaves it up to you.

  • Predictable – Particularly useful in databound controls, this can guarantee uniqueness but gives you control over how that uniqueness is achieved. eg it will append an incrementing value to the end of the generated ClientID or, if you’re using a GridView or ListView, you can append a DataSource field (such as ProductID for example)

For more details on ClientIDs, see this post by Scott Galloway and this post by Matthew Osborn

2 – More Granular ViewState Control

ViewState is a very misunderstood animal” – here here. And you gets what you pays for as they say. It’s funny, ViewState is almost universally disliked – it’s fashionable to dislike it – so why don’t people just turn it off and be done with it? Because it’s incredibly useful, that’s why.

To date though, there has been a problem. ASP.NET lets you turn ViewState off wholesale or leave it enabled but disable it on a control by control basis. What you can’t do is disable it wholesale and just enable it for certain controls. Enter ViewStateMode.

ViewStateMode allows you to disable ViewState at one level in the control hierarchy, eg the Page level, and then re-enable it for a child control(s). This gives you selective control over where ViewState should be enabled and therefore allows you to tune it for maximum benefit with minimum overhead.

For more detail on ViewStateMode see this post by Scott Galloway.

3 – Webforms Routing

ASP.NET Routing was originally developed as part of ASP.NET MVC but factored out into its own assembly (System.Web.Routing) as it’s useful beyond the bounds of MVC. It’s used in ASP.NET Dynamic Data for example and it’s even possible to use it in webforms with a bit of work.

In ASP.NET 4.0 we’ll be shipping the necessary components in the box to use ASP.NET Routing with webforms applications. These include the IRouteHandler implementation (PageRouteHandler class) that serves up the right IHttpHandler to service the request and a couple of expression builders to help capture parameters from routed requests and also generate route URLs. You can just set up you route patterns in Application_Start() as you do for ASP.NET MVC and you should be good to go.

The new expression builders give you access to route parameters and routing URLs from markup. For example, route parameters can be accessed using the RouteValue expression builder so:

<%$ RouteValue:id %> would evaluate to the “id” route parameter value.

When using ASP.NET routing, it’s important that the URLs you generate are “routed” URLs (as these are configurable in Application_Start() and so could change). You need some way to query the framework to find out the correct URL for a given route and set of parameters based on the current RouteTable. The RouteUrl expression builder does this for you. Instead of using an absolute URI, you ask the routing infrastructure to generate the correct URL for the given RouteName and parameters so:

<%$ RouteUrl:RouteName=SearchRte,id=7 %> might evaluate to

There is also a new RouteParameter class which can be used to pass route data as parameters to DataSource controls. Finally there are new HttpRequest.RequestContext and Page.RouteData properties to make it easy to access route information.

For more information on routing, see this post by Phil Haack.

4 – SEO Enhancements

In addition to Webforms Routing (which can help you build nice, clean, SEO friendly URLs), there are a couple of minor additions to ASP.NET 4.0 to make it easier to set the Keywords and Description meta tags on a page.

The page class has new Keywords and Description properties which can be set either in markup (in the Page directive) or from code (allowing you to generate meta tags dynamically should you need to do so)

So for example the page directive:

<%@ Page Language="C#" Keywords="One Two Three" Description="A demo page for keywords and descriptions" %>

or code:

public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Page.Keywords = "One Two Three";
Page.Description = "A demo page for keywords and descriptions";

Would result in the following markup being generated:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta name="description" content="A demo page for keywords and descriptions" />
<meta name="keywords" content="One Two Three" />

For more information see the ASP.NET 4.0 and Visual Studio 2010 Web Development Beta 1 Overview

Technorati Tags:

Comments (4)
  1. ASP.NET/ASP.NET MVC More ASP.NET MVC Best Practices ASP.NET 4.0 Webforms Enhancements Creating jQuery plug-ins from MicrosoftAJAX components CSD/WCF/Azure Cliff Simpkins does for WCF what he did for WF last week, and covers the WCF Changes between Beta

  2. gOODiDEA.NET says:

    .NET Introduction to MapReduce for .NET Developers Working With the ANTS Profiler to Optimize SharePoint

  3. gOODiDEA says:


Comments are closed.

Skip to main content