Daily Archives: February 22, 2008

Set MasterPage (.master) title from a web form (.aspx)

Lots of people complain about not being able to change the master page’s title using ASP.NET 2.0 and up. A commonly seen tutorial is to use the following piece of code:

((HtmlTitle) Page.Master.FindControl("lblTitle")).Text =
    "Set some kind of title";

In most of the cases you need to change the master page’s title, you do not have to bother about this. Just use the correct page stadium and set the page’s title. The only page event to be able to do this is Page_PreInit (since master page and content page are still separate ‘pages’ in this stadium). The following will work (if you have AutoEventWireup enabled ;) ):

protected void Page_PreInit(object sender, EventArgs e)
{
    Title = "Set some kind of title";
}