Dynamically Changing Your Child Page Titles With ASP.NET Master Pages

The primary benefit of ASP.NET master pages is the ability to apply code, themes and templates to child page content. But for some reason, Microsoft did not build in an easy way to concatenate a child page’s title to the master page title.

Consider, for example, dougv.net, my ASP.NET demo site. I want it to take the same titling method as this blog: That is, the title of the page, followed by the domain and the slogan / motto / description of the site.

In the WordPress system, that’s as simple as echoing out some constants in your template file. But there’s no specific control in ASP.NET to set a page title: At least, not a page-based control. You have to hand-code your page titles, and you do that at the child page level — in other words, whatever title you give the child page, that’s the name that shows.

So, if you want to append a slogan or other bit of static text to every page, ostensibly, you’ll need to hard-code it on every page.

Fortunately, we can use server-side code in our ASP.NET master page to append a static bit of text to every page title covered by that master page. All we need to do is capture the hand-coded title of the child page (conveniently, referenced by the Page.Title property), append the text we want to have on all URLs, and then reset the Page.Title value to be the concatenated string.

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
	Page.Title = Page.Title & " » dougv.net » Doug Vanderweide's ASP.NET demos"
End Sub

Note that this technique would also be helpful if you wanted to append certain keywords or key phrases to a page title for search engine optimization (SEO).

For example, you can simply concatenate, to each page, certain keywords or key phrases you want to have appear on all pages covered by a master page.

Or, you could even go more advanced than that. For example, you could use the KeywordToolService from the Google AdWords API, or generate your own master page code to search child page content for common words / phrases within a Content control. Then, just append your results to the child page, as well.

(Of course, you can still access the HtmlMeta class to add keywords and descriptions to a page dynamically. However, most search engines — Google, namely — place little to no value on META keyword lists and less emphasis on the META description of a page than its title / URL.

Leave a Reply

  • Check out the Commenting Guidelines before commenting, please!
  • Want to share code? Please put it into a GitHub Gist, CodePen or pastebin and link to that in your comment.
  • Just have a line or two of markup? Wrap them in an appropriate SyntaxHighlighter Evolved shortcode for your programming language, please!