Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Using CSSCAT to Build CSS-Friendly Web Apps


The HTML markup that many ASP.NET server controls return is sometimes criticized because of the excessive use of <table> tags (which are often nested) and limited CSS styling. In some cases, a table-based layout is just unavoidable — think, for example, of a grid where the number of rows and columns may vary dynamically. In other cases, the same layout for a control can be effectively built using pure CSS-based solutions. This is the case of tree-view and menu controls. Based on the community feedback, the ASP.NET team released a free toolkit to enable a few built-in controls to output CSS-friendly markup where the <table> 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 permissive license that allows for unlimited further customization of the code. CSSCAT is built atop the control adapter architecture that in ASP.NET 2.0 and newer versions makes possible for developers to unplug the default rendering engine and roll your own.

Once you install CSSCAT, you can create CSS-friendly Web sites in Visual Studio. What does this mean to you? The resulting project contains an App_Browsers system folder and a non-empty App_Code folder filled with control adapters source files. The App_Browsers folder contains a file named CSSFriendlyAdapters.browser that is used to map CSS-friendly adapters to some ASP.NET controls. The App_Code folder contains an Adapters subfolder with the source code of all predefined adapters. CSSCAT defines CSS-friendly adapters for the following ASP.NET 2.0 controls: Menu, TreeView, DetailsView, FormView, DataList, GridView, PasswordRecovery, ChangePassword, Login, LoginStatus, and CreateUserWizard. It goes without saying that you can freely edit the .browser file and the App_Code folder to limit the number of CSS-friendly controls in your application. At the same time, by using the source code of CSSCAT as a base, you can start developing new adapters for other controls. The standard Visual Studio project also contains a CSS subfolder with some CSS classes referenced by the CSSCAT new markup. Most of these CSS classes, though, are empty and left to you for further customization.

The effect of CSSCAT on the various controls is different. Let's examine a couple of them — TreeView and GridView. Originally, both controls output a table-based layout. The TreeView generates a sequence of tables in a surrounding DIV. Each table represents a sub-tree and counts three cells and as many rows as there are items to show in the sub-tree. The GridView outputs a plain table with as many rows as there are items to show, and as many columns as bound fields. The header row is rendered as a simple set of <th> tags. Let's take a look at the changes introduced by CSSCAT.

The CSS-friendly TreeView emits a <ul> block for each sub-tree and uses built-in styles to simulate aligned pieces of user interface such as icons, check boxes, and text. The CSS-friendly GridView still uses an overall HTML table, but it is at least padded with more standard and accessible tags such as <thead>, <tbody>, and <tfoot> and styled using CSS classes rather than inline attributes.

If you install the adapter for a particular control, all instances of that control throughout the application will be affected by the adapter. Any style attributes defined directly for the control in the page ASPX source will be ignored.

The benefit of CSSCAT is producing CSS-friendly markup that makes easier to style elements of the user interface and diminish the amount of tables the browser has to work with. CSSCAT is based on the ASP.NET control adapter architecture meaning that you can still use the same approach to make the list of supported controls longer or edit the way in which existing controls render themselves through CSS. For more information on the CSSCAT logic and internal architecture, pay a visit to http://www.asp.net/CSSAdapters/WhitePaper.aspx.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.