Whidbey support for HtmlHead, Intro

Note: this entry has moved.

Very simple questions as “How can I programmatically set my Page’s title from codebehind?” or “How can I handle my Page’s <head> element from codebehind?” were, are and will be asked about every single day in the public newsgroups, forums, etc.

 

This is because v1.x doesn’t include any built-in support for handling the <head> element or any of its children elements.

 

This leads to the common approach of adding the runat=’server’ attribute to the <head> element and then handling it as a HtmlGenericControl. Pretty simple and pretty unfriendly stuff at the same time. In addition, having VS.NET removing the attribute at will just make things more annoying.

 

After looking at all the feedback from newsgroups, forums, etc, it was pretty obvious that something was needed. Because of this, the HtmlHead control was born in the PDC03 bits (and others like HtmlTitle and HtmlLink did appear in the March04 bits). The Page type itself was modified to include these additions and new properties were added: Header (in the PDC03 bits) returns an instance of a type that implements IPageHeader (HtmlHead in this case) and Title (in the March04 bits) just wraps a Header.Title call.

 

So now, this is how you handle this very common need in the Whidbey bits:

 

void Page_Load (object sender, EventArgs e) {

     this.Header.Title = “This is a title”;

     this.Header.LinkedStyleSheets.Add (“classicblue.css”);

     this.Header.Metadata.Add (“author”,”clarius”);
}

 

Pretty simple, ugh? No doubt, this is a good addition. Hopefully all the confusion and how-to questions that plagued the newsgroups about how to deal with this in v1.x won’t be there for v2.0 (now that’s a wish).

 

While playing with this new supported I discovered a few “issues” that I describe in these posts (warning: the following info may be more than you ever wanted to know about <head> and its child elements):

 

Whidbey Support For HtmlHead, Part I – What title will be rendered?

Whidbey support for HtmlHead, Part II – Not only VS.NET messes up your markup

Whidbey support for HtmlHead, Part III – The missing ViewState

 

3 Comments

  • Ah, very cool. Thanks for the tip. I like the detailed investigation part and I'm guessing most of the issues should be worked out by release. This will be better and more organized than having a &lt;asp:literal&gt; sitting in the &lt;Head&gt; of every page.

  • Great low-level detail! Keep the good stuff coming!

  • Suzuki, I can't read japanese... have no idea of what you're saying; hope its not something bad ;-)



    Adam, yes, I'm guessing all these will be worked out by beta or around.



    ScottGu, thanks for the tip!

Comments have been disabled for this content.