OdeToCode IC Logo

The Head Tag in ASP.NET 2.0

Monday, March 26, 2007

The Themes feature in ASP.NET 2.0 requires a server-side <head> tag. In other words, if you don't have runat="server" in the <head> tag, like the following:

<head runat="server">
    <!-- head stuff -->


… then trying to use css files from a theme will create a runtime exception with a clear error message:

Exception Details: System.InvalidOperationException: Using themed css files requires a header control on the page. (e.g. <head runat="server" />).

Not every component yields such a direct message, however.

I've been updating pages in a 5 year old application by adding some AJAX features and using some flair from the Control Toolkit. It's been some time since I've been in the UI layer of an application, but I've restrained myself and haven't gotten too crazy. Still, when I saw the Calendar extender I knew it was a perfect fit. After adding the control, the page threw an exception.

System.NullReferenceException: Object reference not set to an instance of an object.
at AjaxControlToolkit.ScriptObjectBuilder.RegisterCssReferences(Control control) ...
at AjaxControlToolkit.ExtenderControlBase.OnPreRender(EventArgs e) ...
at System.Web.UI.Control.PreRenderRecursiveInternal() ...

Because I knew that the themes feature in ASP.NET 2.0 relies on an active header control to inject style sheets, I figured the ScriptObjectBuilder used the same trick to inject new script tags. Just adding runat="server" in the head tag of the page was enough to fix the problem.