About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

CSS Friendly Control Adapters - Add 'em to Your Site

Recently I've been playing around with the new ASP.NET 2.0 CSS Friendly Control Adapters as I loath the bloated and overly complex table based markup that so many WebControls render by default. After building a test website using the Web Site template that is included with the adapter's v1.0 download I decided to incorporate them into a project I'm currently work on.

There is a good white paper that walks you thru integrating the adapters into your existing web site that I followed step-by-step... almost. First off, my site was using the Web Application Project model, not the Web Site Project model that all the samples use. (I'll post about that later.)

Copy the CSS Files

CSS File The guide says to copy the entire CSS folder (which contains some generic styles that make the controls work) to the root of your site. I didn't want to do this because I like nice, clean directory structures. And seeing as the project already had a Styles directory, I saw no reason to add a redundant CSS directory. It's all about the re-use!

I created a new subdirectory called Adapters, and the entire contents of the CSS folder from the example site into the new directory on my site. All of those style sheets now called <siteroot>/Styles/Adapters/ home.

As the white paper explains, you'll need to add a new <link ... /> element to your MasterPage (or whatever your application uses to wrap all pages) so the browser knows to load those CSS files. Pretty standard operation.

Copy the JavaScript Files

For the same reason that I didn't want create the CSS directory, I didn't want to copy the JavaScript folder into my site root. However, unlike the CSS files I couldn't find any documentation that explained how to tell the adapter code to look in my <siteroot>/Scripts/ directory, rather than the default <siteroot>/Scripts/ <siteroot>/JavaScript/ directory.

Not to fear. I took a quick peak under the hood and saw the following code in the WebControlAdapterExtender class: (sorry the formatting is so bad, but I wanted it to fit on the screen)

string folderPath =
WebConfigurationManager.AppSettings.Get(
"CSSFriendly-JavaScript-Path");
if (String.IsNullOrEmpty(folderPath))
{
folderPath = "~/JavaScript";
}
string filePath = folderPath.EndsWith("/") ?
folderPath + "AdapterUtils.js" :
folderPath + "/AdapterUtils.js";
AdaptedControl.Page.ClientScript.RegisterClientScriptInclude(
GetType(),
GetType().ToString(),
AdaptedControl.Page.ResolveUrl(filePath));

Score! The adapters use a key/value pair from the web.config's appSettings section to determine where the adapter JavaScript lives. All I had to do was add a key/value pair to my application's config file. Here is the one I used:

<add key="CSSFriendly-JavaScript-Path" value="~/Scripts/Adapters/"/>

And just like that, my controls were spitting out lean, clean CSS friendly markup... instead of puking out that garbled table-based crap.

All that was left to do was style my controls. I ended up adding a new CSS file for each control, and then dumped each of those files into the site's App_Themes folder.

What others are saying.

# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Dave Frank
Jan 14, 2007
I hope new versions of ASP.net will include controls that output CSS friendly markup without the need to jump through these hoops!
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Steve Harman
Jan 14, 2007
Agreed!

But I would imagine that it will take a least two pretty major releases of the .NET framework to do so. The first version supporting both rendering methods so UI's based on table based layouts wouldn't break just by upgrading the framework. And then the second release could drop all support for the tables.

Also, I have plans to compile all of the adapters and their resources (CSS & JavaScript files) into an assembly that you can just drop into your application and run. I'll be sure to post about it on this blog.
# CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar DotNetKicks.com
Jan 15, 2007
You've been kicked (a good thing) - Trackback from DotNetKicks.com
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar OmegaSupreme
Jan 16, 2007
These are cool. It used to make me feel dirty to spend hour upon hour making my styles work against all mannor of obscure browsers only to have asp.net dollop a big ol' lump of table nonsense.
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Russ Helfand
Jan 25, 2007
Great review of the adapters! I see that you discovered how to customize the location of the JavaScript files via the web.config. This is actually documented in the adapter kit, albeit in a rather offhanded way in the kit's tutorial. Search that page for "web.config" and you'll find this statement:

"A few of these sample adapters require a small amount of JavaScript. The kit puts this client-side logic into JS files in a folder called JavaScript. For example, TreeViewAdapter.js. However, you can customize this folder location by changing the CSSFriendly-JavaScript-Path key in the appSettings section of the site's web.config file."

On a different note, I see someone mentioned (in the comments above) that they were interested in building a single DLL to encapsulate the whole kit of adapters. This is a terrific idea. You might want to look over Brian DeMarzo's existing work on this same notion(though this page seems to be inaccessible at the moment). Brian commented on his work in Scott Guthrie's blog. Also see this excellent thread describing exactly how to create such a DLL for the adapters on your own.

Best regards... Russ Helfand (author of the CSS Friendly adapters)
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Steve Harman
Jan 25, 2007
@Russ: Thanks for the links and the great work on the CSS Friendly Control Adapters!

One of my favorite features of the adapter kit is that the code is available for everyone to see. (I don't want to call it Open Source, b/c I've not looked to see how it is licensed.) The availablity of code is what allowed me to quickly and easily determine what I needed to do to configure the location of the JavaScript files.

Keep up the good work!
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Brian DeMarzo
Feb 14, 2007
I have a precompiled DLL of the control adapters available on my web site for a one-file (well, almost one-file) drop-in implementation.

http://www.demarzo.net/articles/1056.aspx
# re: CSS Friendly Control Adapters - Add 'em to Your Site
Gravatar Steve Harman
Feb 14, 2007
@Brian: Awesome! I've been meaning to do the same thing, but I've not been able to find the spare cycles.

Thanks for the link, and for the description/instructions on how you went about creating the assembly... and how to use it. Great job!
Comments have been closed on this topic.