How to start from the site map specific node in ASP.NET

The SiteMapDataSource includes two properties which you can use to start site navigation from the specific node StartingNodeOffset and StartingNodeUrl.

StartingNodeOffset

It takes an integer that instructs the SiteMapDataSource to move that many levels down the tree (if the number is positive) or up the tree (if the number is negative). Note: When the SiteMapDataSource moves down the tree, it moves toward the current node. If it’s already at the current node, or your offset takes it beyond the current node, the SiteMapDataSource won’t where to go, and you ’ll end up with a blank navigation control.

To understand how this works, it helps to consider an example. Imagine you’re at this location in a website:

Home > Web Development > HTML and CSS > Scriptable Editing > Reference

If the SiteMapDataSource is starting at the Home node (the default), and you apply a StartingNodeOffset of 2, it will move down the tree two levels and bind the tree from that node down. In this example, that node is HTML and CSS:

HTML and CSS > Scriptable Editing > Reference

That means you’ll be able to jump to any links in the HTML and CSS or Scriptable Editing, but you won’t be able to go anywhere else (at least not without stepping up a level first or clicking another control).

If you attempt to move down too many levels—for example, if the user is on a second-level page and you supply a StartingNodeOffset of 3—the SiteMapDataSource will run out of levels and your bound controls will be left blank.

Another useful technique is to move up from the current node. For example, if you set StartFromCurrentNode to true and use a StartingNodeOffset of -3, the SiteMapDataSource will move up three levels from the current page (Reference) and bind to this tree:

Web Development > HTML and CSS > Scriptable Editing > Reference

This technique is a bit more useful, because it ensures that your navigational controls will always show the same number of levels. If you attempt to step up past the root node, you’ll simply see as many levels as possible. For example, if you specify a StartingNodeOffset of -3 and the user is currently at a second-level page (such as HTML and CSS), you’ll bind to this tree:

Web Development > HTML and CSS

 

StartingNodeUrl

 

It takes the URL of the node that should be the first node in the tree. This value must match the url attribute of the node in the Web.sitemap file exactly. For example, if you specify a StartingNodeUrl of “./ WebDevelopment.aspx”, then the first node in the tree is the Web Development node, and you will see only nodes underneath that node. The property is useful if you want to vary between a small number of different site maps, no more than nine or ten. The best solution is to define multiple site map files and bind to the one you want to use. Because the default XmlSiteMapProvider supports only a single site you should separate the different site maps into distinct branches of the Web.sitemap file.

For example, imagine you want to have a Development tools section and a .NET development section on your website. You might split this into two different structures and define them both under different branches in the same file, like this:

<siteMap xmlns=”http://schemas.microsoft.com/AspNet/SiteMap-File-1.0″ >

<siteMapNode title=”Root” description=”Root” url=”./”>

<siteMapNode title=” Development tools home” description=”Home” url=”./default.aspx”>

</siteMapNode>

<siteMapNode title=”.NET development home” description=”Home” url=”./default_net.aspx”>

</siteMapNode>

</siteMapNode>

</siteMap>

Now, to bind the menu to the Development tools view, you set the StartingNodeUrl property to “./default.aspx”. You can do this programmatically or, more likely, by creating an entirely different master page and implementing it in all your dealer pages. In your .NET development pages, you set the StartingNodeUrl property to “./default_net.aspx”. This way, you’ll show only the pages under the .NET development home branch of the site map. You can even make your life easier by breaking a single site map into separate files using the siteMapFile attribute, like this:

<siteMap xmlns=”http://schemas.microsoft.com/AspNet/SiteMap-File-1.0″ >

<siteMapNode title=”Root” description=”Root” url=”./”>

<siteMapNode siteMapFile=” DevelopmentTools.sitemap” />

<siteMapNode siteMapFile=” NETDevelopment.sitemap” />

</siteMapNode>

</siteMap>