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

LINQ to Web 2.0


ASP.NET Controls and the Zen of CSS Design

In the early days of HTML design and web design, people thought that <table> tags were the holy grail of design because you could position anything you wanted in tabular fashion. Thankfully we learn from our mistakes, as you can see with CSS Zen Garden (www.csszengarden.com), which illustrates the use of <div> and <span> tags, and Cascading Style Sheets (CSS) to produce extremely lightweight, yet compelling and beautiful web interfaces. Additionally, designing in a CSS "Zen" fashion makes your application more pliable via client-side JavaScript and DHTML technologies, such as Ajax or Atlas.

Unfortunately, many of the controls that ship as part of the stock suite in ASP.NET 2.0 have missed the boat as far as CSS Zen design is concerned. These controls litter your output with HTML tables, rows, columns, and cells, and even embed style information directly in the tag instead of using CSS classes! To see what I mean, the hideousness in Listing One is produced by an ASP.NET 2.0 GridView using "Auto Format" (which should be renamed "Auto Munge") to create a "pretty" grid.

<table cellspacing="2" cellpadding="3" rules="all" 
  border="1" id="GridView1" 
  style="background-color:#DEBA84;border-color:#DEBA84; 
    border-width:1px; border-style:None;">
  <tr style="color:White;background-color:#A55129;font-weight:bold;"> 
     <th scope="col">title</th> 
     <th scope="col">url</th> 
     <th scope="col">tags</th></tr>
  <tr style="color:#8C4510;background-color:#FFF7E7;"> <td>This is a sample bookmark</td> <td>http://www.sample.com</td>
<td>sample test</td> 
</tr> 
</table>
Listing One

The preceding HTML output was for a GridView control that was autoformatted to "brown sugar." one of the simpler formats. There isn't a single use of a style sheet here, nor is there any straightforward way of making the elements of the GridView conform to a CSS-pure design (I've been told it's possible, but it involves far more work than should be necessary).

Would it have been so difficult for Microsoft to produce a div/span grid that corresponded to a stock suite of CSS classes and let developers override the grid's styling using CSS? I now have a requirement that I must justify why I couldn't use divs and spans before I check in an .aspx page that renders tables. ASP.NET—and specifically Web 2.0—would be a better place if everyone followed the same rule. To illustrate, take a look at the HTML in Example 1 that shows a single bookmark in a CSS-friendly fashion.

<li class="xfolkentry"> 
  <div class="link"> 
    <a href="http://www.slashdot.org">Slashdot</a> 
  </div> 
  <div class="description"></div> 
     <div class="meta"> <span class="postdate">8/28/2006</span> 
       ... more text clipped for article ... 
  </div> 
</li> 

Example 1: CSS-friendly HTML.

You can then apply any style sheet imaginable to create millions of different look-and-feels for this output. In addition to the CSS-friendly output being easier to read, more flexible, and more pliable by professional web designers, it also downloads faster and provides a page that is more lightweight than one where every aspect of each row of data's style is embedded directly on that row of data. One possible look for the preceding HTML is in Figure 1, where the preceding HTML was rendered using the Scuttle style sheet.

Figure 1: Example 1 rendered using the Scuttle stylesheet.

In general, controls that let you supply output templates can be tweaked to produce CSS-friendly output. However, controls such as DataList actually wrap individual row templates within table rows and cells—making that control completely useless. Also, watch out for the GridView control and many of the stock Web Part controls; they're heavy abusers of tables and are gathering some very bad CSS karma.

What I've found is that the time it takes to convert some of the stock controls (such as GridView) to produce CSS-friendly output is twice the time it takes to produce your own CSS-friendly output (including all grid functionality, especially if you use Atlas for inline editing!) in a Repeater.


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.