LANGUAGES: C# | VB.NET
ASP.NET VERSIONS: 2.0
Server Controls Improved
What s New in the Building Blocks of ASP.NET 2.0 Applications
By Dino Esposito
Server controls are the building blocks of any ASP.NET application. Any change to a control s architecture may have ramifications on the whole application which can be both good and bad. New features added to a base class can benefit a slew of top-level controls; changes in the internal behavior of a base control can affect the programming style of other controls. In worse cases scenarios but thankfully not in ASP.NET changes to building blocks like this can also break applications. For this reason, it s important to take a look at the structure of ASP.NET 2.0 server controls to see what s new and what has changed.
In the transition from ASP.NET 1.1 to ASP.NET 2.0, server controls gained some new programming features, such as themes, browser-specific rendering, and support for input focus. This is the tip of the iceberg, though. Below water, in fact, and invisible at the page-authoring level, you ll find a number of other improvements with a more architectural connotation, including XHTML 1.1 conformance, adaptive rendering, and control state. Let s take a look at each of them, one at a time.
XHTML is a World Wide Web Consortium (W3C) standard that admits Web pages only if tagged as well-formed XML documents. This approach guarantees that the elements in the pages of a Web application are more forward-compatible with browsers in the near future and, more importantly, should be able to work in the same way regardless of the browser. It is common today to have quite forgiving browsers to guess and replace a missing tag, while others, more rigorously, don t render the page if there s something wrong or missing. This is the historical heritage of HTML, designed to be simple, but de facto made even simpler by certain browsers one name, Internet Explorer even though with the best intentions. So join me in heartily welcoming XHTML in ASP.NET 2.0.
ASP.NET 2.0 defaults to XHTML 1.1, but you can also opt for XHTML 1.0 Strict or Transitional. The rendering engine of some controls has been reworked to make them produce compliant output. As a result, in ASP.NET 2.0, each control s markup includes a closing tag. Furthermore, the viewstate hidden field comes surrounded by a
Another point of XHTML weakness for ASP.NET 1.x pages are script tags. In ASP.NET 2.0, a script tag always includes the type attribute and is rendered inside a CDATA element.
For the most part, these changes won t affect migrated applications. There might be some exceptions, however. For example, think of a page that relies on the now ceased name attribute of the form. To turn off XHTML 1.1 compliance, add a new setting to the web.config file:
It forces ASP.NET 2.0 to render controls as in ASP.NET 1.x, ignoring the XHTML compliance directive. In this way, you allow yourself time to rework the various pages to XHTML while keeping the application working. You should not abuse this setting, though, as it might not be supported in future versions of ASP.NET.
It is essential to note that ASP.NET 2.0 guarantees the generation of XHTML-compliant output for the vast majority of core ASP.NET server controls, but not for all. HyperLink, BulletedList, and AdRotator controls are at risk because of their internal and required use of the target attribute. For example, the following markup fails W3C validation:
In general, all controls and custom controls that incorporate a child hyperlink with the target attribute set automatically break the XHTML compliance of the host page. In some cases, GridView and TreeView might also not be XHTML 1.1-compliant. If you make use of third-party controls, check with the vendor to see what they say about XHTML.
ASP.NET is, per se, unable to fix static text in the page that breaks compliance. To help developers inserting XHTML-compliant HTML elements, Visual Studio.NET supports validation of XHTML markup for static elements, as shown in Figure 1.
Figure 1: Choosing the target validation schema for markup in Visual Studio.NET 2005.
While your management and marketing people welcome the XHTML support because it can open up new possibilities for ASP.NET applications, as a developer you might be concerned that XHTML 1.1 support ends up generating markup hard to digest for some browsers. Internet Explorer 6.0, for example, is not XHTML 1.1-compliant, but recognizes well XHTML 1.0 Transitional. The same holds true for Netscape 7.x and Opera 7.x. What will happen when an ASP.NET 2.0 page gets hit by one of these?
Internally, the adaptive rendering engine of ASP.NET 2.0 controls checks the browser s capabilities and generates markup that works around the browser s XHTML shortcomings. As a side note, consider that the markup of ASP.NET 2.0 controls is also fully compatible with Section 508 accessibility guidelines.
Adaptive rendering is the process that enables controls to generate different markup for individual browsers. This result is obtained by delegating the generation of the markup to an external component: the adapter. When each control is about to render, it figures out its current adapter and hands the request over to that adapter.
The adapter for a control is resolved by looking at the browser s capabilities as configured in the ASP.NET configuration folder. If the browser record includes an adapter class for that control, the class is instantiated and used. Otherwise, the adapter for the control is an instance of the base ControlAdapter class. The ControlAdapter class is a generic adapter and simply generates the markup for a control by calling the rendering methods on the control itself.
In ASP.NET 2.0, browser information is stored as a list of text files with a .browser extension. Text files are located under the ASP.NET installation folder on the Web server; following is the exact path:
A control holds a reference to the mapped adapter instance through the Adapter property (a protected property). Each control has an associated adapter unless it is a composite control that defers to its children for rendering.
In ASP.NET 2.0, control properties can take values that change according to the underlying browser. To do so, you use a special syntax that consists of a browser-specific prefix added to the property name: