Click here to Skip to main content
Click here to Skip to main content

Site Navigation in ASP.NET 2.0

By , 20 Sep 2005
 

Sample Image - SiteNavigation0.jpg

Introduction

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="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">http://schemas.microsoft.com/AspNet/SiteMap-File-1.0</A>" >
Structure is obvious just I describe some attrubitues:

url Relative address of page
title Demonstrates the title of page
description Short 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.

SiteMapViewType

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

Tree Site map date will be viewed as tree hierachy
Flat Site map data will be viewed as flat strucutre. It's useful for some special controls like drop down list.
Path Using 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.

StartingNodeType

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

Current All nodes from current node to its childs
Parent All nodes from parent of current node to its childs
Root All 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.

Menu

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:Menu>
<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"
 Orientation="Horizontal">
</asp:Menu>
<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:
<StaticHoverStyle>
<StaticMenuItemStyle>
<StaticMenuStyle>
<StaticSelectedStyle>
<StaticTemplate>
<DynamicHoverStyle>
<DynamicMenuItemStyle>
<DynamicMenuStyle>
<DynamicSelectedStyle>
<DynamicTemplate>

SiteMapPath

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">
</asp:SiteMapPath>

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:

ParentLevelsDisplay

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">
</asp:SiteMapPath>

Result: (PPC.aspx?cat=PPCPE)

SiteMapPath control

PathSeparator

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=" - ">
</asp:SiteMapPath>

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)

PathDirection

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

TreeView

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:TreeView>
<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:

CurrentNode Gives back current page
RootNode Gives back root page of current page
ParentNode Gives back parent node of current node
Title Gives 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

Bugs

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

History

  • 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.

License

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWeb sitemapPathmemberdabolo18-Nov-12 14:31 
Dear you,
 
If I have web.sitemap and web2.sitemap, I want sitemapPath active web2.sitemap.
How can I?
 
Help me pls!
Thanks!
QuestionMaking the hole field clickable?memberthomasa14-May-09 3:58 
Is it possible to make the hole field clickable?
 
Now, only the text fires the click event.
Questioncan you help me handle this?memberNika Asgari6-Oct-08 19:32 
hi keyvan
im trin to use sitemappath but a little diffrent
i have a menu that use sitemap and i dont want my sitemappath use that sitemap
 
so i need 2 sitemaps
i cant make sitemappath use it
will you help me?
 
Nika Asgari


Be a Man of changes.

GeneralDynamic Menu control using sitemapdatasource without using web.sitemap in C#memberMember #376777227-Jan-07 2:03 
HI,
I am working on project where i got the necessity of dynamic menu using menu control in asp.net2.0 without using web.sitemap.
please help me. Thanks in advance.
 
regards
Seshu
 
regards
Seshu

QuestionRegarding menu control using sitemapmemberkumarcs21-Sep-06 19:30 
Hi, this one is gr8 article to start with sitemap stuff.
Thanks to the author.
 
I have a question. Is there anyway to include a small image in front of the menus which is bound from the sitemap stuff. For example, take this article:: In front of Home Page I need to insert an image like ">>". Please letme know is there any way to do that.
 
Thanks,
kumar.
GeneralMenu control with many root level itemsmembereggsovereasy7-Jun-06 10:37 
Is there a way to generate the menu control from a sitemap, but have many top level items on the menu? The way it is now, I could have 1 menu item called "Menu" then under it have "File Edit View etc", whereas, like at the top of my browser, I want many smaller menus.
 
I would however, still like the SiteMapPath to show the root level item.
GeneralRe: Menu control with many root level itemsmembereggsovereasy8-Jun-06 3:34 
I figured it out set ShowStartingNode = false on the SiteMapDatasource
AnswerRe: Menu control with many root level itemsmemberGreg Kniffin15-Jun-06 11:27 
This problem can be solved by specifying the 'staticdisplaylevels' property of the menu control.   Setting this to '2' will display the root node and all the child nodes of the root (but not the children of the child nodes).   Consider the following:
 

<siteMap>
   <siteMapNode url="default.aspx" title="Home"   description="Home">
      <siteMapNode url="about.aspx" title="About"   description="About">
         <siteMapNode url="sub1.aspx" title="Sub 1" description="" />
         <siteMapNode url="sub2.aspx" title="Sub 2" description="" />
      </siteMapNode>
      <siteMapNode url="blog.aspx" title="Blog"   description="Blog" />
      <siteMapNode url="contact.aspx" title="Contact"   description="Contact" />
      <siteMapNode url="photos.aspx" title="Photos"   description="Photos" />
   </siteMapNode>
</siteMap>
 
Setting the staticdisplaylevels = 2 will produce the following results:
 
   Home   About   Blog   Contact   Photos
 
If you don't want to display the root node, you can do as you described by setting the showstartingnode property to false.
 
Greg Kniffin
<a href="http://www.gregkniffin.com/">www.gregkniffin.com</a>
GeneralThat's exactly what I was looking for!memberAlexandru Savescu23-Apr-06 15:58 
Thanks
 
Regards,
Alexandru Savescu
 
http://www.savescu.net[^]
 


Generalmenu control fire pop up windowsussAnonymous4-Oct-05 3:19 
do you know how to fire a pop up window instead of going to URL when you click a menu item.
Thanks
Fred
AnswerRe: menu control fire pop up windowmemberKeyvan Nayyeri4-Oct-05 15:59 
No at present but work on it to find a good method. Although I think that you can do it with Atlas package (And its Popup control) but would test it asap Wink | ;)
 
Keyvan Nayyeri
Website: www.nayyeri.net
GeneralMenu ControlmemberGfw21-Sep-05 7:54 
Do you know how to tie the menu control to the sitemap.xml file and be able to link to a new (_blank) window? There appears to be a target property in menu if using menuitem, but I can't seem to find if using the menu control tied to SiteMapDataSource. Thanks.
 
Gfw
 

AnswerRe: Menu ControlmemberKeyvan Nayyeri21-Sep-05 16:37 
If you want to bound menu control to a XML file you can do something like this:
 
<asp:Menu ID="Menu1" runat="server" DataSourceID="XMLDataSource1"
Orientation="Horizontal">
<DataBindings>
   <asp:MenuItemBinding DataMember="siteMapNode" TextField="title" />
   <asp:MenuItemBinding DataMember="siteMapNode" TextField="url" />
</DataBindings>
</asp:Menu>
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/Web.sitemap" XPath="siteMap/siteMapNode"></asp:XmlDataSource>
 
But about targeting your items to new window I couldn't do it. Dynamic, Static and Level menu items have many properties to get CSS styles (Inline or from a separate CSS file) but as you know opening links in new windows is wrong based on W3 rules.
And last point: I've used XMLDataSource control to bind my Menu to a XML file. XMLDataSource can't support NameSpaces (It's a bug) so if you want to use this control take care about its errors. For more information about XMLDataSource control you can read my article here:
http://www.codeproject.com/aspnet/XMLDataSource.asp
 
Keyvan Nayyeri
Website: www.nayyeri.net
GeneralRe: Menu ControlmemberDoug Wilson2-Jan-06 3:13 
To target your items to a new window, bind the target field to the target attribute of your XML row.
 
If you specify _blank as the target value on your datasource, then it will launch the item in a new window.
 
i.e. on you datasource xml:
 
<siteMapNode url="mypage.aspx" title="New Window" target="_blank" />
 
and then on your databindings:
 
<asp:MenuItemBinding DataMember="siteMapNode" TextField="title" NavigateUrlField="url" TargetField="target" />
 
Also, make sure you have a target attribute on each xml row, or it won't be able to bind when executing. Leave the value blank to open in current window.
 
Hope this helps.
 
Doug Wilson
GeneralRe: Menu ControlmemberKeyvan Nayyeri19-Jan-06 16:25 
Thank you for this tip Wink | ;)
 
Keyvan Nayyeri
Website: www.nayyeri.net
GeneralRe: Menu ControlmemberAnna Williams24-May-06 3:26 
Thanks for the tip! I tried using target = blank and it didn't work. I didn't realize that each of the rows needed to have the target parameter or that I had to set the targetfield of the menu control.
 
Thank you for posing this.
GeneralRe: Menu ControlmemberDanno31219-Dec-06 12:30 
Another solution I found without using target at all. Simply use javascript. example:
 
in the sitemap nodes put url="javascript:onNodeClick('test.aspx');"
 
and put this script on your page
 
function onNodeClick(obj){
window.open(obj,'_blank');
}
 

 
Cheers

GeneralRe: Menu Controlmemberken4296526-Dec-06 6:40 
Thank you! Thank you! Thank you! Danno!
 
I've tried 15 different things and spent a couple of hours trying to get the previous suggestions on this thread to work and yours worked perfectly in just a couple of minutes!!!
 

 
Ken
GeneralRe: Menu ControlmemberReddyPaduri9-Mar-07 14:40 
Simple & Best Solution.

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

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