Wednesday, 18 August 2010

Empty title tag added when adding an HtmlTitle into a placeholder

I just spent too much time trying to work out why the HEAD tag of a site I was given contained two TITLE entries, and therefore wasn't validating. The head of the masterpage contained a user control that consisted of a single placeholder, to which was being dynamically added a title control like so:

HtmlTitle title = new HtmlTitle();
title.Text = Title;

where the variable Title is a string value sent up from specific content page.

It turned out that because the master page's HEAD markup didn't contain any TITLE tags, ASP.NET was surreptitously adding one for me despite me adding one programmatically during the page's build. Stepping through the code when the page was loading didn't show this. It just kind of did it.

I found this hack to put a top to this strange behaviour. I put this in the HEAD tag on the master page. I'm adding the tags to keep it happy, but setting it to visible="false" so we don't see it:
<title id="Title1" visible="false" runat="server"><%-- hack to turn the auto title off --%></title>
Dirty but effective.


  1. Thanks for the info, I had this crop up today.

  2. thank you for this hack, it came up on a site i've been working on for a long time. It works perfectly.


Comments are moderated, so you'll have to wait a little bit before they appear!