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

The article How to skip the root node from the site map in ASP.NET teaches you how you can skip the root node. This article will teach you how you can show just a portion of the complete size map, starting from the current node.  For example, you might use a control such as the TreeView to show everything in the hierarchy starting from the current node. You can implement this design by setting the SiteMapDataSource.StartFromCurrentNode property to true. You still have the choice of whether to use ShowStartingNode, but now it determines whether you show the current node, because that’s the starting point for the navigation tree. The next markup lines show the revised Site.master and Web.sitemap files:

 

Site.master file

<head id=”Head1″ runat=”server”>

<title></title>

<asp:ContentPlaceHolder ID=”head” runat=”server”>

</asp:ContentPlaceHolder>

</head>

<body>

<form id=”form1″ runat=”server”>

<table>

<tr>

<td style=”width: 230px; vertical-align: top;”>

<!– Navigation controls go here. –>

<asp:TreeView ID=”treeNav” runat=”server” DataSourceID=”SiteMapDataSource1″ />

</td>

<td style=”vertical-align: top;”>

<asp:ContentPlaceHolder ID=”ContentPlaceHolder1″ runat=”server” />

</td>

</tr>

</table>

<asp:SiteMapDataSource ID=”SiteMapDataSource1″ runat=”server” ShowStartingNode=”true” StartFromCurrentNode=”true” />

</form>

</body>

Web.sitemap file

<?xml version=”1.0″ encoding=”utf-8″ ?>

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

<siteMapNode title=”Root” description=”Root” >

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

<siteMapNode title=”Web development” description=”Web development” url=”./WebDevelopment.aspx”>

<siteMapNode title=”HTML and CSS” description=”HTML and CSS” url=”./HTMLandCSS.aspx” />

<siteMapNode title=” Scripting” description=”Scripting” url=”./Scripting.aspx” />

</siteMapNode>

 

<siteMapNode title=”Enterprise servers and development” description=” Enterprise servers and development we offer” url=”./EnterpriseSD.aspx”>

<siteMapNode title=”Mail servers” description = “Mail servers” url=”./MailServers.aspx” />

<siteMapNode title=”SQL servers” description=”SQL servers” url=”./SQLServers.aspx” />

<siteMapNode title=”Application servers” description=”Application servers” url=”./ApplicationServers.aspx” />

</siteMapNode>

 

</siteMapNode>

</siteMap>

The next picture shows the result:

 

Binding to child nodes only

Binding to child nodes only

 

Note:

For this technique to work, ASP.NET must be able to find a page in the Web.sitemap file that matches the current URL. Otherwise, it won’t know where the current position is, and it won’t provide any navigation information to the bound controls.