Julie Lerman's DevLife

DevLife Part I [May 2005 - March 2007]

My Links

Blog Stats

News

A blog for DevSource.com.

This blog was originally part of the blogs.ziffdavis.com site from May 2005 through June 2007 when the blog was moved to the Movable Type blog engine and hosted at blog.devsource.com/devlife.
The original blog was eventually shut down and I was given the posts so that I could host them on my own site.


Archives

Have no fear of ASP.NET Control Adapters

ASP.NET 2.0 is all about extensibility. One of the extensibility points is provided by Control Adapters. I have long ignored them, assuming they were for gurus only. Now that I have played with them, I realize they are really easy to comprehend and to work with.

When you place a regular HTML control on a web page, it goes straight through to the page that ends up in the end-user's web browser. But when you place an ASP.NET server control on your web page, it is merely an instruction to the ASP.NET processor to output HTML that looks like what you asked for. The beauty of this is if you want something with the complexity of a GridView (not only in how it looks on the page, but with the ability to write server side code to interact with it), you don't have to build up all of the crazy HTML necessary to get it on a browser page. ASP.NET does that for you.

But what if you don't like the way ASP.NET renders GridViews on your page? That's where Control Adapters come in. You can create a control adapter that says "hey, every time I use an ASP.NET GridView, don't let ASP.NET render it the normal way. Instead, here's how *I* want it rendered. You define this in your own code that uses an HTMLTextWriter to build up the HTML that you want. Then  you use a configuration file that maps the System.Web.UI.WebControls.GridView to your Adapter Class (eg TheDataFarm.MyWebUI.Adapters.MyGridView). You can even define different adapters based on browsers. So in the mapping file, you can say “map the GridView to TheDataFarm.MyWebUI.Adapters.IE.GridView when the browser is IE“. Then you can have a separate mapping to TheDataFarm.MyWebUI.Adapters.FF.GridView when the browser is FireFox.

That's a really high level explanation of Control Adapters. The ASP.NET Team's Sandbox projects contain an API that has Control Adapters for five web server controls. The project is called “CSS Friendly Control Adapters”. These adapters were built to render HTML that is more easily formatted with CSS than with the default Tables that ASP.NET renders for them. They are a great introduction to Control Adapters and the source code is available so you can see how the custom adapter classes are coded in addition to how things are wired up. Working with this project was how I was first introduced to Control Adapters.

posted on Monday, July 10, 2006 11:56 AM