Click here to Skip to main content
15,885,048 members
Articles / Web Development / ASP.NET

Generate Web Page Titles from your Site Map

Rate me:
Please Sign up or sign in to vote.
3.82/5 (6 votes)
23 Jan 2007CPOL4 min read 40.9K   291   32   7
Site maps aren't just for navigation—they contain titles and descriptions of all your web pages. Use those descriptions to title your web pages.

Introduction

Though many web sites use a site map for navigation, site maps can be handy for generating web page titles, too! If you're not using a site map for navigation, you still might want to create one just for page titles. However, if you are already using a site map, you are ten minutes away from doubling its usefulness!

In this article, I describe a method for utilizing the page titles and descriptions in a site map to title your web pages by programmatically altering the HTML <title> tag. In addition to increasing the use of your site map, it places all your page titles in one convenient to edit location!

Background

ASP.NET Site Maps

ASP.NET site maps contain a hierarchal layout of the pages in your web site along with a title and description attribute for each page. The most common use for site maps is to produce a "tree-view" of the layout of a site, or produce the "bread crumb" navigation you are most likely familiar with as pointed to by the mouse cursor in the image below:

Web Page Titles from Site Map Data

The title attributes from your site map are used for the navigation links. The description attributes—if provided—appear as tool tips when a link is hovered on by the cursor. Because you have already provided this information in your site map, why not use it to title your web pages as well? In the image above, you can see that I have!

Google and Search Engines

Search engines such as Google return listings with the exact title of your web page as a link in bold print:

Google Search Engine Listing

These bold print headlines are the item that catches viewers' eyes and gets them to click on your site. Make your titles good! Additionally, Google places high priority on the keywords in your page titles when ranking listings. Again, pick good titles!

The Code—Ten Minutes to Completion

To begin, your site should use .master pages (or some other method) so that the HTML <head> and <title> tags are in one place instead of replicated on each individual page.

Programmatically Changing the <title> Tag

To programmatically access and modify the page title, you must use the ASP.NET header control: <head runat="server" />. In my master page, I also specify a default title that is used for all web page titles:

HTML
<head runat="server">
  <title>Tek4 Innovations</title>
  ...
</head>

Modifying the <title> element is now trivially accomplished with the following code:

VB
Page.Title = "My Web Page Title"

Override the Page_Load Method

In the same master page, I override the Page_Load method to append site map information to the page title—with a few twists:

C#

C#
<script runat="server">
  void Page_Load(Object sender, EventArgs e) {
    // Set page title based upon SiteMap Description or Title

    SiteMapNode currentNode = SiteMap.CurrentNode;
    if (currentNode != null) {
      String subTitle = currentNode.Description;
      if (subTitle.Length == 0) subTitle = currentNode.Title;
      if (subTitle.Length !=0 && subTitle != "Home") 
        Page.Title += "\u2014" + Server.HtmlEncode(subTitle);
    }
  }
</script>

VB

VB
<script runat="server">
  Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode
    If Not currentNode Is Nothing Then
      Dim subTitle As String = currentNode.Description
      If subTitle.Length = 0 Then subTitle = currentNode.Title
      If subTitle.Length <> 0 And subTitle <> "Home" Then
        Page.Title &= "\u2014" & Server.HtmlEncode(subTitle)
      End If
    End If  
  End Sub
</script>

The code appends the specific page title to the default title already present in the <title> tag, inserting the em-dash '—' character (Unicode "\u2014") as a separator. The description attribute from the site map usually contains the longer and more complete description, so that is the preferred title. If description is empty, then the contents of the site map's title attribute is used. The code leaves the default title alone if there is nothing to append.

One additional caveat: I didn't want my home page to say "My Web Site—Home". I simply wanted "My Web Site". Therefore, my code performs a special test for the "Home" page and disregards appending anything. I could have left the title attribute in my site map blank, but I'm using the site map for navigation as well, so that wasn't an option—I still need a "Home" link in my navigation!

Points of Interest

The problem with using site maps is you are forced to constantly update the site map so that every page is listed. If you forget to list a page, suddenly you have a page with no title! Worse, if you are using site maps for navigation, the navigation menu disappears when you browse to the page!

To ease site map maintenance—and cover for when I forget—I created a SiteMapProvider class that dynamically generates site map entries for unlisted pages, "inheriting" the description from the "parent" page. Therefore, if you forget to add your "Widgets" page to your site map, but it is in the "Our Products" page directory, then the page would still receive the page title, "Our Products", which is better than nothing.

Take a look at my ScionSiteMapProvider here: ScionSiteMapProvider.

History

  • 2007-Jan-18: Original post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Kevin Rice is a firefighter for a major Southern California fire department. When not working, he enjoys web programming, administration, riding dirt-bikes (Visit SLORider.com) or building anything electronic.

Comments and Discussions

 
GeneralMy vote of 3 Pin
minakshimallick20-Feb-13 22:40
minakshimallick20-Feb-13 22:40 
GeneralKeywords and description Pin
Saleh Orainy5-Jan-09 18:16
Saleh Orainy5-Jan-09 18:16 
GeneralRe: Keywords and description [modified] Pin
kriceslo5-Jan-09 20:07
kriceslo5-Jan-09 20:07 
QuestionSurely you'd do it the other way round? Pin
piers723-Jan-07 0:30
piers723-Jan-07 0:30 
AnswerRe: Surely you'd do it the other way round? Pin
kriceslo23-Jan-07 11:15
kriceslo23-Jan-07 11:15 
GeneralVB correction Pin
Johann Frot22-Jan-07 23:02
Johann Frot22-Jan-07 23:02 
GeneralRe: VB correction Pin
kriceslo23-Jan-07 11:06
kriceslo23-Jan-07 11:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.