Displaying a Breadcrumb in the Page's Title

Published 20 July 06 06:31 PM | Scott Mitchell

ASP.NET 2.0 site navigation features includes a breadcrumb web control (SiteMapPath) that, when added to a page that exists within the site map, displays output like:

Home > Products > Books > Fiction

Where the nodes in the breadcumb are links back to the respective sections. Internally, the SiteMapPath control finds the current site map node being visited and walks up the site map to the root, generating the output. The SiteMapPath is great for displaying a breadcrumb as text in a web page, but wouldn't it be nice to have the web page's title automatically set to the same breadcrumb text? (The page's title is what appears in the browser's title bar and is the default text used when bookmarking a page.)

It's really quite easy to add this functionality to your site. The site map's structure and data can be accessed programmatically via the SiteMap class. If you're using a master page, you can just drop the following code in the Page_Load event handler, otherwise you'll need to add this on a page-by-page basis:

1 // put the "default" title here

2 string title = "Welcome to My Website!";


4 if (SiteMap.CurrentNode != null)

5 {

6 SiteMapNode current = SiteMap.CurrentNode;

7 title = current.Title;

8 current = current.ParentNode;


10 while (current != null)

11 {

12 title = string.Concat(current.Title, " :: ", title);

13 current = current.ParentNode;

14 }

15 }


17 // finally, set the page's title to the title variable

18 Page.Title = title;

That's all there is to it! Initialize the title variable to the value you want displayed if the currently requested page is not in the site map. To set the page's title, simply use Page.Title (see Dynamically Setting the Page's Title in ASP.NET 2.0 for more info).

The tutorials in my Working with Data in ASP.NET 2.0 series just leave the pages' titles set to “Untitled Page“. In hindsight, I wish I would have added the above code snippet into the master page in the Master Pages and Site Navigation tutorial, so instead of seeing “Untitled Page“ we'd see something like:

For more information on ASP.NET 2.0's site navigation system, check out my multi-part series on 4Guys, Examining ASP.NET 2.0's Site Navigation.

Filed under:


No Comments


My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.