ASP.NET Web Forms and XHTML
Monday, February 11, 2008 at 12:02AM |
Kevin Rohrbaugh I'm looking forward to the ASP.NET MVC framework, mainly because my experience with Struts and Rails (while admittedly limited) has left me with an appreciation of the pattern. I'm especially looking forward to using a different view engine from Web Forms, as I'm not a fan of the control model when you get into AJAX use.
All that said, there's a lot of Web Forms applications in existence today and the model has some powerful benefits, most notably the ability to easily slap controls on a page and provide some fairly sophisticated functionality with very little mark-up generated by the developer. Of course, not having control over the mark-up can be an issue as well, especially when it comes to web standards and cross-browser compliance. Here's what I do to get the cleanest and most-compliant mark-up out of ASP.NET with the least hassle.
Step 1: Set a valid DocType
The first part of writing compliant mark-up is always to tell the browser you're doing so. A valid doctype essentially requests that browsers parse your page using standards mode, as opposed to quirks mode. While far from perfect on lots of browsers (IE6, I'm looking at you) standards mode is more consistent across various browsers than quirks, and you want to be using it. I like to use XHTML 1.0 Strict or XHTML 1.1 myself, but the W3C and A List Apart has more information on doctypes, so I won't go into too much detail. The default since VS2005 is XHTML 1.0 Transitional, which will get you out of quirks.
Step 2: Set your HTML Validation Schema
Within Visual Studio, there's a few different schemas for error checking mark-up in the designer. Since we've decided to write standards-compliant mark-up, we should let Studio in on the plan, so it can help us when we write those <img> tags. This is done by selecting the Target Schema for Validation from the HTML Source Editing toolbar. There's a few options in there that correlate to the doctypes above, and I believe the default is XHTML 1.0 Transitional in VS2005+.
Step 3: Set your XHTML Conformance Level
As I stated earlier, the Web Forms development model yields an environment where much of the mark-up sent to the browser is generated by the ASP.NET runtime itself. This means the runtime should also be instructed as to the type of mark-up to output. Fortunately, Microsoft added this capability in ASP.NET 2.0, with the xhtmlConformance element in web.config. It informs ASP.NET to output mark-up targeted at various standards, including Legacy, Transitional and Strict, the latter two correlating, once again, to the doctypes mentioned above and Legacy basically causing controls to output as they did in ASP.NET 1.1 and should be avoided if at all possible. The default setting for ASP.NET since 2.0 is Transitional.
Bonus Points: Use the CSS Friendly Control Adapters
Along with the shift to better structured mark-up, web standards have also shifted to a model of separating presentation information from content, through the use of Cascading Style Sheets (CSS). Using CSS to style ASP.NET controls was made a lot easier with the release of the CSS Friendly Control Adapters. If you're at all interested in using CSS on some of the more complicated controls, these are also worth checking out. While not specifically related to XHTML compliance, they tend to make the generated mark-up a bit cleaner and easier to work with. These should also work with ASP.NET 3.5, but was not built into the framework itself so you'll still need to download and install.
Wrapping Up
If you plan on using XHTML 1.0 Transitional, the default settings for most of the steps above will get you there, so while it seems like a lot of information, most of it is already done for you. If you want to use a stricter doctype, you still have options, which is a much better position than where we were in ASP.NET 1.1. All that said, I'm still looking forward to the alternate view engines that will be available to us in ASP.NET MVC. While Web Forms were great in a post-back centric world, AJAX and my time in Rails has left me wanting more direct control over my mark-up. Let's hope we hear more definite announcements regarding MVC's release soon.

Reader Comments