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.
Related Posts
- Dynamically Adding JavaScript onLoad Event-Driven Scripts To ASP.NET 2.0 Templated / Master Page Content Pages (51.6)
- Update: Dynamically Adding JavaScript To Select ASP.NET Content Pages / Master Pages Via The HtmlGenericControl Class (49.2)
- Displaying An Image Stored In A SQL Server Database On An ASP.NET Page Using VB.NET (21.1)
- Getting Plain Text From An ASP.NET 2.0 Page For Use As An AJAX Data Source (19.7)
- Using National Weather Service XML Feeds With ASP.NET, ADO.NET And XSL (14.6)
The numbers inside parentheses are relevance scores. Scoring is based, in order of priority, on title, category, content and tags. The higher the score, the more likely that post relates to this post.


Comments Closed