The straightforward way to use the same title for all pages if you are using master pages is to 1) set the <title> in the master page's HTML and 2) remove it from every content page's page directive. That works great. But I want to make this a little easier to maintain, so I hooked this into my site's base page and use web.config to maintain the site's title.
My site's base page (BasePage), from which all of my site's pages are derived, is declared like this:
public class BasePage : System.Web.UI.Page
In the BasePage constructor, I get the site's title from web.config and trap for any exception:
public BasePage()
{
try
{
Title = WebConfigurationManager.AppSettings["SiteTitle"];
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message);
Title = "My Site";
}
}
This also allows me to forget about manually removing "Untitled Page" from the page directive in every content page. To programmatically set the title, should I need to, I expose a property that prevents setting it to blank (or to the default Visual Studio .NET setting):
public new string Title
{
get
{
return base.Title;
}
set
{
if (value.Trim().Length > 0 && value != "Untitled Page")
{
base.Title = value;
}
}
}
Now, to maintain my site's title, I just set it in web.config:
<add key="SiteTitle" value="My Site" />
So this gives me a fairly clean way to maintain my site's title and also to override it per content page should I need to. For example, my contact page can append " - Contact Us" to the existing title by modifying the base page's Title property in Page_Load.
Seems like a lot of work for something so simple. I'm sure there may be a cleaner way, but for now this seems fairly good.