Thursday, July 22, 2010

HtmlTextWriter fluent interface

When working on new features in IZWebFileManager I needed to change rendered HTML.

I took a look at the code I wrote more then 3 years ago.... It is so complicated!

IZWebFileManager is a typical custom control that overrides Render and uses HtmlTextWriter's API to produce HTML. Here is a small part of the rendering flow:



It took me a while to figure out what is HTML is rendered here.
The fact is, that HtmlTextWriter's API is not intuitive and requires a lot of effort to produce relatively simple HTML. Is there a alternative way to render output?

WhenLINQ has been released all the .NET world has met fluent APIs, and I was wandering if there is such an API for rendering ASP.NET controls.

I found this one: http://csharpfeeds.com/post/9989/HtmlTextWriter_fluent_interface.aspx

I've just added it to my project (made it internal to prevent collisions), changed it a bit and rewrote the code above:



Now it is much easier to change the rendered HTML, because this code flow is much closer to the HTML markup flow, than when it was written using the "classic" HtmlTextWriter API.