Enabling greater control over rendered HTML

A discussion (and some early thoughts) on enabling greater control over server-generated markup in ASP.NET...
Dimitri Glazkov has some interesting thoughts around ASP.NET controls, and the perceived lack of control over the rendered markup and prompted me to respond. To summarize, Dmitri suggests proposes a more generalized usage of adapters that would be an abstraction layer for rendering, and a more flexible mechanism for selecting them. Bertrand followed this up with a post of his own on the level of control over the rendered output where he brought up a range of options from CSS to templates to themes to custom control development as mechanisms for greater control over the rendering. Check out their posts for more details.

In general, I agree with some of the thoughts Dmitri has - the ability to make rendering (and processing postback data etc.) completely pluggable is desired, and would be nirvana to some. I have myself been having similar thoughts as I think about where we should go in the future. At the same time, I am not convinced adapters are the panacea one might be looking for. A key problem with adapters is exposing internal state of the control to the adapter. It is possible to expose some state, but the moment an external developer decides to write a new adapter, it is likely they will find they need some piece of internal state information that has not been exposed to them. The adapter model essentially creates a parallel inheritance hierarchy, which doesn't work all that great for extensibility in the long run.

There are a number of other things to consider and balance with the ability to have greater control over rendering as well - namely simplicity, and performance.

The Login control is a great example of these needs balanced. The login control on the page, and very simply set up some properties to customize the look and behavior. The result is a fully functioning login scenario with some control over the behavior, and modest control over the look and feel. This sort of functionality is absolutely essential. Requiring the page developer or designer to select an adapter or a theme or anything else would be an obstacle in the control's usability. However, the control does allow the page designer to peel a layer away and completely customize the content using a template. The templating approach leverages the compositional paradigm, which is easier to understand than the adapter model. One important implementation detail in this approach is how the Login control uses the content of the template. It doesn't look for specifically a control of type TextBox. Instead it looks for a control that implements ITextControl. This allows the page developer to use a custom text-box control if they prefer that over the standard one. The Login control's default rendering can also implemented as a template - so the decision to use a custom template does not come with a significant performance penalty.

Combined with themes, the Login control metaphor is pretty compelling, and goes a long way in providing greater control over the final rendering. We do need to apply it more generally where appropriate.


One place where ASP.NET does not offer a whole lot of control over rendering is in defining markup structure, especially in repeating scenarios, such as in TreeView or GridView. The logic to generating this repeating structure is encapsulated deep inside the control, and isn't customizable or replaceable beyond the set of properties provided by the control. Templates are great to customize the content shown within a structure, but do not provide the ability to specify structure. I think this is an area where "adapters" and "structural templates" might infact be extremely useful, but there are a number of problems to solve. Some of my thoughts in this area have been inspired by new concepts built into Avalon - a great avenue for some future prototyping and blog posts.


Another interesting point Dmitri brought up that hasn't been discussed much is the possibility of distinct tool experiences for developers and designers. I do think one of the fundamental benefits of server controls that remains largely untapped is in the area of tool experience. By virtue of being "living" entities within the design surface, server controls have the ability to offer different experiences based on the context in which the design surface is being used. More thought on this end needs to be given as well.
Posted on Friday, 8/6/2004 @ 3:26 PM | #ASP.NET


Comments

5 comments have been posted.

Andrey Skvortsov

Posted on 8/13/2004 @ 10:49 AM
DropDownList dosen't support customization through item templates mechanism and it's very frustrating because I can't customize look and feel of rendered ListItems when use data binding(it even dosen't expose ItemCreated event how some other controls do).So,I think templating mechanism is very attractive from every possible perspective and easy to use,it must become standard way to extend/customize rendering mechanism of any control abstract model.

Bertrand Le Roy

Posted on 8/13/2004 @ 5:21 PM
Andrey: what would you do with item templating in a DropDownList? It will be rendered as an HTML SELECT, which only allows for text in the items. So using a format string in your databinding expression should be all you need in this case.

Nikhil Kothari

Posted on 8/13/2004 @ 7:05 PM
The HTML <select> tag is a prime example of a primitive browser element that shows off its age (and might I say lack on innovation in this space). I agree it would be nice to be able to customize the content of a DropDownList - we'd just need to start with a custom implementation of a drop-down element that uses <div>'s and floating windows for its implementation. There is probably something to be said for consistency of basic form elements across pages, though judicious uses (eg. Font picker with a font preview ala Word) do exist.

Andrey Skvortsov

Posted on 8/16/2004 @ 1:26 AM
Bertrand,select element has collection of "option"s which is customizable by itself and have styling capabilities.I want to customize look of particular "option" element based on some data source field,so it's not simple data binding problem but problem with more wide approach to data binding.In my case I want to bind presentation of control to data source- not only "DataTextField"/"DataValueField",some kind of theming/styling through data source.
Thanks,Nikhil.I feel the same about this and has already wrote such <div> based combobox with dynamic filtering of contents using XMLHTTP(no callbacks,sorry,I don't need opaque strings) as IE's attached behavior it looks pretty nice;-)So how about support for element/attached behaviors for IE customized/optimized rendering of control,it's marketing step to attract users/developers to IE IMHO-code become more manageable/compact with behaviors(I'm always using smartNavigation setting in our corporate net-more seamless user experience;-)

Sanjay kattimani

Posted on 10/12/2005 @ 7:52 AM
Whenever you use smartnavigation, make sure you use absolute paths in your scripts, or else css gets ripped off. Take a look at my blog on this topic http://sanjaykattimani.blogspot.com/2005/10/net-smartnavigation.html
The discussion on this post has been closed. Please use my contact form to provide comments.