-[Danny Chen]- Blog of an ASP.NET QA tester

Tips and info about Site Navigation, ImageMap, Menu and other cool ASP.NET v2.0 features.

Why does StartingNodeOffset make my SiteMap dissappear?

This used to confused me too before I finally understood what was happening.  First, let me explain what I'm talking about.  Let’s say our SiteMap looks like this:



And in home.aspx we have the following:

<asp:SiteMapDataSource runat="server" id="SiteMapDataSource1"
    StartingNodeOffset="1" />
<asp:TreeView runat="server" id="TreeView1"
    DataSourceID="SiteMapDataSource1" />

Here's the output:



Now, the same code in page4.aspx and in page10.aspx


So what happened?  Well, the SiteMapDataSource needs a new root node for the tree because we requested a StartingNodeOffset.  In order to determine this new root node, ASP.NET takes the path to the current node and chooses the next node down from the root node as the new root node.  From there, ASP.NET can derive a sub tree that the SiteMapDataSource should represent.



But, in the case of home.aspx, we were at the last node in the chain.  The exact same thing would happen if we did this: 

<asp:SiteMapDataSource runat="server" id="SiteMapDataSource1"
    StartingNodeOffset="1" StartFromCurrentNode="true" />. 

The result is that ASP.NET did not have enough nodes in the path to get the new root node, so 'Nothing' was returned and no tree gets rendered:

[StartingNodeOffset="5", page10.aspx]

So when would StartingNodeOffset and StartFromCurrentNode="true" ever be useful together?  StartingNodeOffset doesn't have to be positive, it can also be a negative number.  Using: 

<asp:SiteMapDataSource runat="server" id="SiteMapDataSource1"
    StartFromCurrentNode="true" StartingNodeOffset="-3" />

will only show the tree from 3 levels above and downward from wherever you are and thus filtering out potentially large amounts of non-relevant data. 

Comments

Ben said:

Wow... not received a comment in a looong time!

Well thanks very much for this, i appreciate it :)

# September 2, 2007 9:46 AM

Brian said:

Like it. Hope you post more .NET, Visual Studio tips.

Thanks

# November 15, 2007 7:42 PM

George said:

Thanks for this explanation!

Two questions: I cannot see why HOME + 1 gives "nothing" here (2005_04_04_nothing.jpg) and page 8 there (2005_04_04_nothing2.jpg). And why is home.aspx the "last node in the chain"? It looks like the top node in the chain to me.

# March 7, 2008 4:38 AM

Dynamicaly changing site map | keyongtech said:

Pingback from  Dynamicaly changing site map | keyongtech

# January 21, 2009 11:40 PM

Mikeldi said:

Hey! That was really useful i was pulling out my hair dealing with the menus. Works like a charm in Sharepoint 2010 asp:SiteMapDataSource, Thanks dude! :D

# January 23, 2012 9:15 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)