Click here to Skip to main content
13,549,950 members
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 24 Aug 2005

Site Navigation in ASP.NET 2.0

, 20 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Describes how to use Site Navigation capabilities in ASP.NET 2.0

Sample Image - SiteNavigation0.jpg


Site Navigation was one of major problems in website programming with ASP and ASP.NET early versions.
Changing the location of a page or a resource lead to problems for internal links. Old solutions were not able to solve these problems.
ASP.NET 2.0 is coming with new features and controls to solve them. These features produced big enhancement in Site Navigation.
In this tutorial I'm going to describe these new features and site navigation controls group for you.
I use a sample website about smart devices in this tutorial.

Site map definition

Site Map definition (as I'll describe later) is not necessary to use with Site Navigation but it's common and useful.
In this section we define the structure of our site in a XML file which we use it for SiteMapDataProvider in next sections.
Now we add new file to our project that has .sitemap extension. Default name for this file is Web.sitemap. It has simple structure that you can see here:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="<A href=""></A>" >
    <siteMapNode url="Default.aspx" title="Home Page" 

  description="Home Page Of My Site">
  <siteMapNode url="PPC.aspx" title="Pocket PC" 

   description="About Pocket PC">
   <siteMapNode url="PPC.aspx?cat=WinMobile" title="Win Mobile For PPC" 

    description="About Windows Mobile" />
   <siteMapNode url="PPC.aspx?cat=PPCPE" title="PPCPE" 

    description="About PPCPE" />
   <siteMapNode url="PPC.aspx?cat=Software" title="Softwares" 

    description="About Pocket PC Softwares" />
  <siteMapNode url="SmartPhone.aspx" title="Smart Phone"

   description="About Smart Phones">
   <siteMapNode url="SmartPhone.aspx?cat=WinMobile" 

    title="Win Mobile For Smart Phone"

    description="About Windows Mobile" />
   <siteMapNode url="SmartPhone.aspx?cat=Game" title="Smart Phone Games"

    description="Latest Games for Smart Phones" />
  <siteMapNode url="News.aspx" title="News" 

   description="Latest News">
   <siteMapNode url="News.aspx?cat=PPC" title="PPC News"

    description="Latest News About PPC" />
   <siteMapNode url="News.aspx?cat=SmartPhone" title="Smart Phone News"

    description="Latest News About Smart Phone" />
Structure is obvious just I describe some attrubitues:

urlRelative address of page
titleDemonstrates the title of page
descriptionShort decription about page

SiteMapDataProvider control

Many of Site Navigation controls use this DataProvider to connect to SiteMap file.
Usually this control uses a tree hierarchy to view data. But there are other methods for this purpose. I'm going to describe them here with their properties and values.


This property demonstrates the method to view SiteMap file. It can get these values:

TreeSite map date will be viewed as tree hierachy
FlatSite map data will be viewed as flat strucutre. It's useful for some special controls like drop down list.
PathUsing this propery leads to viewing site map file as node to root strucutre. You'll see some thing similar to this later in SiteMapPath control.


This property demonstrates the order of showing nodes and can get these values:

CurrentAll nodes from current node to its childs
ParentAll nodes from parent of current node to its childs
RootAll nodes from root to childs. This is default property

Site Navigation controls

I don't want to describe each control and its properties. Just try to show how can they be used in site navigation and don't focus on their properties and methods and stylistic tags.


Surely this is one of useful added controls to ASP.NET 2.0. You can use it as easy as other navigation controls. Just add a SiteMapDataSource to your page and set its ID as DataSourceID property of your Menu control.
Attend to this code:

<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

And its result: (SmartPhone.aspx)

Vertical Menu control

You can change the layout of this control with Orientation property. It can get two values: Horizontal and Vertical. By default you use Vertical.

<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

Result: (SmartPhone.aspx)

Horizontal Menu control

Also you can change the style of this control with following tags but it's not in my goals here:


Using this control is very simple. After that a SiteMapDataProvider is in place you can add this control to your page then view your page in browser.
Here you can see the code for PPC.aspx page (It will generate automatically):

<asp:SiteMapPath ID="SiteMapPath1" runat="server">

And this is the result:

SiteMapPath control

If you hover on different parts of this control you can see a description as ToolTip. ASP.NET uses your description attribute in Web.sitemap file for this page as ToolTip text.
Important properties of this control are:


If you have a site with many pages then you can define the number of parent nodes before current node with this property:

<asp:SiteMapPath ID="SiteMapPath1" 
 runat="server" ParentLevelsDisplayed="1">

Result: (PPC.aspx?cat=PPCPE)

SiteMapPath control


This control uses > character as default separator between links. You can use this property to change this character. Just look at this code:

<asp:SiteMapPath ID="SiteMapPath1" 
 runat="server" PathSeparator=" - ">

This is the result: (PPC.aspx)

SiteMapPath control

Note that you can use <PathSeparatorStyle> to change the style of your separator (Like Background color, Font color, etc)


This property can get two values: RootToCurrent and CurrentToRoot. By default you use RootToCurrent. If you change it to CurrentToRoot direction of your links will be changed.

SiteMapPath control


Working with this control is as simple as working with Menu control. Add a SiteMapDataSource to your page and set its ID as your TreeView control DataSourceID:

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

Run the page and see this: (News.aspx)

Tree view control

This control has many properties and methods those need another article. This new version of TreeView control is really enhanced in ASP.NET.

Site Navigation in code

Not only you can deal with Site Navigation capabilities through controls but also you can work with them through your code.
Look at following table. It contains important properties in Site Navigation API:

CurrentNodeGives back current page
RootNodeGives back root page of current page
ParentNodeGives back parent node of current node
TitleGives back the title of current node (You had demonstrated it in site map file)

This code will show you how to use above properties:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    lblResult.Text = "Current Title: " & SiteMap.CurrentNode.Title & "<br>" & _
    "Parent: " & SiteMap.CurrentNode.ParentNode.Title.ToString & "<br>" & _
    "Root: " & SiteMap.CurrentNode.RootNode.ToString() & "<br>" & _
    "NextSibling: " & SiteMap.CurrentNode.NextSibling.ToString & "<br>" & _
    "Previous Sibling: " & SiteMap.CurrentNode.PreviousSibling.ToString & "<br>"
End Sub
Result: (SmartPhone.aspx)

Site Navigation in code


When I tried to add SiteMapPath control to PPC.aspx page I got an error in this control but page worked properly.


  • 2005/08/25 - I posted article. This article is written by ASP.NET 2.0 Beta 2 so I'll update it with RTM version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Keyvan Nayyeri
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
For more information about me just visit my blog @ Nayyeri.NET

You may also be interested in...


Comments and Discussions

Generalmenu control fire pop up window Pin
Anonymous4-Oct-05 3:19
sussAnonymous4-Oct-05 3:19 
AnswerRe: menu control fire pop up window Pin
Keyvan Nayyeri4-Oct-05 15:59
memberKeyvan Nayyeri4-Oct-05 15:59 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 20 Sep 2005
Article Copyright 2005 by Keyvan Nayyeri
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid