Categories
ASP.NET Software Engineering

Why is the HtmlHead class sealed?

ASP.NET 2.0 gives us a Page.Title property, which we can set in code, or in the Page directive. Great! Unfortunately, I had a requirement so that whilst I’d be setting a portion of the title from the page, the rest would be pre-defined (ideally within the master page that I use). Obviously you can’t fiddle the stuff in the server-side title tag, because the contents just gets overriden. So I thought I’d just extend the new System.Web.UI.HtmlControls.HtmlHead class and fiddle the Title property to add the required string to the end Except you can’t. Because its sealed. Why? And does anyone have another way to do this?

6 replies on “Why is the HtmlHead class sealed?”

I hate to state the obvious here that you should favor (or favour in your native tounge) composition over inheritance. But I just did it anyway 😉

public class MyCrazyHtmlHead : HtmlGenericControl

{

private HtmlHead internalHead;

public MyCrazyHtmlHead(HtmlHead head)

{

this.internalHead = head;

}

…..You get the idea.

}

You could expose a Title property in your (master) page and use that to set the title. In the implementation of the title”s setter you could then combine the provided value with a pre-defined value, and update the HtmlHead”s Title property accordingly.

Declaratively, you should then be able to set the page title by doing something like:

<%@ Page Title="Hello World" %>

This should call the Title property”s setter in your page.

Thanks for your comments guys!

Chris,

I”m pretty sure that composition doesn”t work in this case. The HtmlHead control registers itself with the Page object internally – so the Page object gets a direct reference to that HtmlHead object, and sets the Title property directly.

Wilco,

For that, as far as I understand it, although I could expose, say a "MasterTitle" property, I could only set it through code in my pages, and would have to set the MasterType directive (which irritatingly can”t be set globally in the web.config) in order to get a strongly typed instance…. which seems far from elegant.

Am I missing something obvious here?

Bleh. 🙂

It works perfectly fine.

using System;

using System.Web.UI;

using System.Web.UI.HtmlControls;

public partial class MyCrazyHtmlHead : UserControl

{

private HtmlHead internalHead;

private string titlePart;

protected void Page_Load(object sender, EventArgs e)

{

internalHead = Page.Header;

}

protected override void OnPreRender(EventArgs e)

{

internalHead.Title = string.Format("{0} {1}", internalHead.Title, TitlePart);

base.OnPreRender(e);

}

public string TitlePart

{

get { return titlePart; }

set { titlePart = value; }

}

}

Of course you”ll have to tweak it out to fit your needs. And probably change it to a HtmlGenericControl. But, it does work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.