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

# re: Displaying a Breadcrumb in the Page's Title 7/30/2008 8:16 AM Saad Elsherif

Hi Scott,
it works only when i put this code in the Page_load event handler of the content page!? But when i put it in the page_load event handler of the master page, it doesn't affect any content page that uses this master page!??

Thanks a lot for your support!

Saad,
saad.elsherif@yatfund.org

# Dynamischer Title | hilpers 1/20/2009 6:20 AM Pingback/TrackBack

Dynamischer Title | hilpers

# Four Helpful Features to Add to Your Base Page Class 2/21/2009 5:47 PM Continuous.Integration

Four Helpful Features to Add to Your Base Page Class

# Dynamically Setting the Page's Title in ASP.NET 2.0 6/17/2009 11:37 PM 海洋——海纳百川,有容乃大.

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

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles