ASP.NET: Override HtmlHead Rendering with ControlAdapters
2 weeks ago or so I posted about the problematic rendering of HtmlHead server control in MasterPage - MasterPage HtmlHead Renders Messy HTML. Design-time tags rendering is messed up not to mention additional tab characters and spaces inside the title tag... I think that all core Rendering implementation out there has problems. I also totally don't understand the need to render tab characters.
Fortunately this behaviour can be overriden using control adapters. All credit goes to Bazile for the idea and control adapter samples - see original solution in the comments to my entry.
The implementation is like so:
- Create control adapters classes (class that inherits from System.Web.UI.Adapters.ControlAdapter):
- HtmlHeadAdapter
- HtmlLinkAdapter
- HtmlMetaAdapter
- HtmlTitleAdapter (My 2 cents - instead of rendering title inside HtmlHeadAdapter)
- Override Render method in each one of them.
- Create folder App_Browsers in the root of your web application.
- Create a file named Default.browser under App_Browsers.
Example of Default.browser implementation:
< browsers> < browser refID="Default"> < controlAdapters> < adapter controlType="System.Web.UI.HtmlControls.HtmlHead" adapterType="Onecone.Web.ControlAdapters.HtmlHeadAdapter" /> < adapter controlType="System.Web.UI.HtmlControls.HtmlTitle" adapterType="Onecone.Web.ControlAdapters.HtmlTitleAdapter" /> < adapter controlType="System.Web.UI.HtmlControls.HtmlLink" adapterType="Onecone.Web.ControlAdapters.HtmlLinkAdapter" /> < adapter controlType="System.Web.UI.HtmlControls.HtmlMeta" adapterType="Onecone.Web.ControlAdapters.HtmlMetaAdapter" /> < /controlAdapters> < /browser> < /browsers>
That's it.
Wednesday, January 31, 2007 12:28 AM