Programming Microsoft ASP.NET 4 - Dino Esposito [124]
for the Menu for whatever browsers the user will employ:
adapterType="Core35.MenuAdapter" />...
Saved to a .browser file, the preceding snippet is deployed to the App_Browsers folder of an ASP.NET application.
An adapter class looks like the following class:
public class MenuAdapter :
System.Web.UI.WebControls.Adapters.MenuAdapter
{
...
}
The class commonly overrides methods such as Init, RenderBeginTag, RenderEndTag, and RenderContents.
To write an adapter effectively, though, you must reasonably know a lot of details about the internal workings of the control you’re hooking up. For more information on the architecture of control adapters, you might want to take a look at http://msdn2.microsoft.com/en-us/library/67276kc5.aspx.
This is only half the problem, however.
Getting CSS-Friendly HTML
The markup that too many ASP.NET server controls return makes excessive use of
tags (often nested) and inline style properties. Subsequently, ASP.NET controls make limited use of CSS styling. It might be easier and quicker, sure, but it’s probably a shortsighted approach.Based on community feedback, the ASP.NET team first released a free toolkit to enable a few built-in controls to output CSS-friendly markup where the
tag is not used or used less and in accordance with XHTML rules. The CSS Control Adapter Toolkit (CSSCAT) can be downloaded from http://www.asp.net/cssadapters. It comes with full source code and a permissions license that allows for unlimited further customization of the code. CSSCAT is built atop the control adapter architecture of ASP.NET.CSSCAT defines CSS-friendly adapters for the following controls: Menu, TreeView, DetailsView, FormView, DataList, GridView, PasswordRecovery, ChangePassword, Login, LoginStatus, and CreateUserWizard. By using the source code of CSSCAT as a starting point, you can develop new adapters for other controls. For more information on the CSSCAT logic and internal architecture, pay a visit to http://www.asp.net/cssadapters/whitepaper.aspx.
ASP.NET 4 supports two rendering mechanisms: legacy and CSS-friendly. You control the rendering mechanism for all pages in the application using the controlRenderingCompatibilityVersion attribute added to the section in the configuration schema. You can set the attribute with one of the following two strings: “3.5” or “4.0”....
If you set it to “3.5”, rendering will occur as in older versions of ASP.NET. If you set it to “4.0”, a number of controls (Menu, GridView, Image) will automatically render out cleaner and much more CSS-friendly HTML. It’s still not perfect, but it’s definitely a much better option, especially if you consider that all you need to do is add a line to the configuration file.
In ASP.NET 4, the Control class (and subsequently the Page class) features a new property, RenderingCompatibility, that informs you about the selected rendering machinery. It’s key to notice that although the RenderingCompatibility property has a setter method, that is reserved for ASP.NET and using it programmatically doesn’t necessarily result in visible effects. In other words, the following code compiles but doesn’t produce any results:
// Default is 4.0
this.RenderingCompatibility = new Version(3, 5);
So for your own purposes, you should consider RenderingCompatibility to be a read-only property and resort to the section to change the rendering algorithm for all controls in all application pages. The default rendering version is 4.0 if you choose to create an ASP.NET 4 application.Let’s see the most relevant example of CSS friendliness enforced in ASP.NET 4. Here’s the Menu control as it is being used in the sample ASP.NET project template: