Scott on Writing

Musings on technical writing...

Displaying a Breadcrumb in the Page's Title

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!";

    3 

    4 if (SiteMap.CurrentNode != null)

    5 {

    6     SiteMapNode current = SiteMap.CurrentNode;

    7     title = current.Title;

    8     current = current.ParentNode;

    9 

   10     while (current != null)

   11     {

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

   13         current = current.ParentNode;

   14     }

   15 }

   16 

   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.

posted on Thursday, July 20, 2006 6:31 PM

Feedback

# re: Displaying a Breadcrumb in the Page's Title 7/28/2006 6:48 AM John Dyer

Scott,
From what I've read on search engine optimization, it's best to put the title of the page first, so perhaps you could just reverse the order...

# Requesting Article | Iran 8/12/2006 1:52 PM mustafa zamany

With Hello ! i am Producing a Weblog service. I want to add template design like blogger.com to Weblog service that someone could edit his/her own template Please write an article about this and PLEASE Let me know e-mail me at ZMSTFA@GMAIL.COM

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37914.0%
Tuesday 45316.7%
Wednesday 50518.6%
Thursday 53319.6%
Friday 49418.2%
Saturday 1666.1%
Total 2716100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1043.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1585.8%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.4%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2716100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.32419
Wednesday 7.69638
Thursday 6.90607
Friday 5.48411
Saturday 5.27158
Total 5.852716

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.83328
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.67286
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 10.50147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.852716

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles