Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SiteMapDataSource.StartingNodeOffset Property

Gets or sets a positive or negative integer offset from the starting node that determines the root hierarchy that is exposed by the data source control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
public virtual int StartingNodeOffset { get; set; }
<asp:SiteMapDataSource StartingNodeOffset="Int32" />

Property Value

Type: System.Int32
The default is 0, which indicates that the root hierarchy exposed by the SiteMapDataSource is the same as the starting node.

If the StartingNodeOffset property is set to a value other than 0, it affects the starting node and with it the site map data hierarchy that is exposed by the SiteMapDataSource control. The negative or positive integer value of the StartingNodeOffset identifies the number of levels up or down the site map hierarchy from the starting node that is identified by the StartFromCurrentNode and StartingNodeUrl properties to offset the starting node of the subtree that is exposed by data source control.

If the identified starting node is the node that represents the currently requested page, the StartingNodeOffset is ignored.

If the StartingNodeOffset property is set to a negative number -n, the starting node of the subtree that is exposed by the data source control is the ancestor node n hierarchical levels above the identified starting node. If the value n is greater than the number of ancestor levels in the hierarchical tree, the starting node of the subtree is the root node of the site map hierarchy.

If the StartingNodeOffset property is set to a positive number +n, the starting node of the subtree that is exposed is a child node n levels below the identified starting node. Because more than one branch of child nodes might exist in the hierarchy, the SiteMapDataSource attempts to resolve a child node directly on the path between the identified starting node and the node that represents the currently requested page, if possible. If the node that represents the currently requested page is not in the subtree of the identified starting node, the value of the StartingNodeOffset property is ignored. If the node that represents the currently requested page is less than n levels below the identified starting node, the currently requested page node is used as the starting node.

The value of the StartingNodeOffset property is stored in view state.

The following code example demonstrates the effect of the StartingNodeUrl and StartingNodeOffset properties. The example consists of several ASP.NET pages, a master page, master page section, and a Web.sitemap file. The Web.sitemap file defines the hierarchical structure of the site. For this code example, it defines a number of pages identified by unique URLs, such as 1.aspx, 2.aspx, A.aspx, and B.aspx, as follows:


<siteMap>
    <siteMapNode url="Default.aspx" title="Root"  description="" roles="">
        <siteMapNode url="A.aspx" title="A"  description="" roles="">
            <siteMapNode url="1.aspx" title="1"  description="" roles="" />
            <siteMapNode url="2.aspx" title="2"  description="" roles="" />
        </siteMapNode>
        <siteMapNode url="B.aspx" title="B"  description="" roles="">
            <siteMapNode url="3.aspx" title="3"  description="" roles="" />
            <siteMapNode url="4.aspx" title="4"  description="" roles="" />
        </siteMapNode>
        <siteMapNode url="C.aspx" title="C"  description="" roles="">
            <siteMapNode url="5.aspx" title="5"  description="" roles="" />
            <siteMapNode url="6.aspx" title="6"  description="" roles="" />
        </siteMapNode>      
    </siteMapNode>
</siteMap>

The Site.master page contains the SiteMapDataSource and a TreeView controls inside a ContentPlaceHolder control. It identifies the starting node of the exposed hierarchy as Default.aspx, using the StartingNodeUrl property. The Site.master file is used by the Default.aspx page only.

<%@ Master Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="SiteMap" Runat="server">
                Pick a node:<br /><br />
                <asp:TreeView ID="TreeView1" Runat="server" ShowExpandCollapse="false" DataSourceID="SiteMapDataSource1" MaxDataBindDepth="1" />
                <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" StartingNodeUrl="~/Default.aspx" ShowStartingNode="false" />
            </asp:ContentPlaceHolder>
            <asp:ContentPlaceHolder ID="MainBody" Runat="server"/>
        </div>
    </form>
</body>
</html>

All other pages, such as 1.aspx, 2.aspx, A.aspx, and so on, use the Section.master master page. This master page is very similar to Site.master, however it contains several SiteMapDataSource controls that you can experiment with. The TreeView control that displays site map data is initially bound to SiteMapDataSource1, which specifies a StartingNodeOffset of 1. This indicates that the parent node of the currently requested page is displayed. If you view page 1.aspx, the hierarchy displayed by the TreeView is A, 1, 2. However, if you view page 4.aspx, the hierarchy is B, 3, 4.

If you change the DataSourceID for the TreeView to SiteMapDataSource2, the TreeView control displays site map data differently. When you use SiteMapDataSource2, the StartFromCurrentNode property is set to true, which means that the site map data is displayed relative to the currently requested page and the StartingNodeOffset property is set to -1, indicating that one level of child nodes is displayed, when possible. When you view the A.aspx page, the hierarchy that is displayed is the hierarchy of the current level, including all siblings, such as B and C. When you view 1.aspx, the parent nodes are no longer displayed.

<%@ Master Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">

    Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            Dim currentNode As DropDownList = sender
            currentNode.SelectedValue = System.Web.SiteMap.CurrentNode.Url
        End If
    End Sub

    Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim currentNode As DropDownList = sender
        Response.Redirect(currentNode.SelectedValue)
    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ContentPlaceHolder ID="SiteMap" Runat="server">
                <b>Current Location:</b>
                <asp:SiteMapPath ID="SiteMapPath1" Runat="Server">
                    <CurrentNodeTemplate>
                        <asp:DropDownList ID="DropDownList1" DataTextField="Title" DataValueField="Url" AutoPostBack="true"
                            OnDataBound="DropDownList1_DataBound" DataSourceID="SiteMapDataSource2" Runat="server"
                            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
                    </CurrentNodeTemplate>
                </asp:SiteMapPath>
                <br />
                <br />
                <a href='<%= System.Web.SiteMap.RootNode.Url %>'>Back to Top</a>
                <br />
                <br />
                <asp:TreeView ID="TreeView1" ShowExpandCollapse="false" Runat="server" DataSourceID="SiteMapDataSource1" />
                <asp:SiteMapDataSource ID="SiteMapDataSource2" Runat="server" StartingNodeOffset="-1"
                    ShowStartingNode="false" StartFromCurrentNode="true" />
                <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" StartingNodeOffset="1" />
            </asp:ContentPlaceHolder>
            <asp:ContentPlaceHolder ID="MainBody" Runat="server" />
        </div>
    </form>
</body>
</html>

The following code example demonstrates one of the ASP.NET pages you can use with the other components of this code example.

<%@ Page MasterPageFile="~/Section.master" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID=MainBody Runat=Server>
  <h2>1.aspx</h2>
</asp:Content>

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.