SiteMapPath Class

Displays a set of text or image hyperlinks that enable users to more easily navigate a Web site, while taking a minimal amount of page space.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class SiteMapPath : CompositeControl
<asp:SiteMapPath />

The SiteMapPath control is a site navigation control that reflects data provided by the SiteMap object. It provides a space-saving way to easily navigate a site and serves as a point of reference for where the currently displayed page is within a site. This type of control is commonly called a breadcrumb, or eyebrow, because it displays a hierarchical path of hyperlinked page names that provides an escape up the hierarchy of pages from the current location. SiteMapDataSource. The SiteMapPath is useful for sites that have deep hierarchical page structures, but where a TreeView or Menu might require too much space on a page.

The SiteMapPath control works directly with your Web site's site map data. If you use it on a page that is not represented in your site map, it will not be displayed. For more information about site maps, see ASP.NET Site Navigation Overview.

The SiteMapPath is made up of nodes. Each element in the path is called a node and is represented by a SiteMapNodeItem object. The node that anchors the path and represents the base of the hierarchical tree is called the root node. The node that represents the currently displayed page is the current node. Any other node between the current node and root node is a parent node. The following table describes the three different node types.

Node type

Description

root

A node that anchors a hierarchical set of nodes.

parent

A node that has one or more child nodes, but is not the current node.

current

A node that represents the currently displayed page.

Each node displayed by a SiteMapPath is a HyperLink or Literal control that you can apply a template or style to. The templates and styles are applied to nodes according to two rules of precedence:

  • If a template is defined for a node, it overrides any style defined for the node.

  • Templates and styles that are specific to types of nodes override general templates and styles defined for all nodes.

The NodeStyle and NodeTemplate properties are applied to all nodes, regardless of their node type. If both these properties are defined, the NodeTemplate takes precedence.

The CurrentNodeTemplate and CurrentNodeStyle properties are applied to nodes that represent the currently displayed page. If a NodeTemplate is defined in addition to the CurrentNodeTemplate, it is ignored. If a NodeStyle is defined in addition to the CurrentNodeStyle, it is merged with the CurrentNodeStyle to create a merged style. This merged style uses all the elements of the CurrentNodeStyle, plus any additional elements of the NodeStyle that do not conflict with the CurrentNodeStyle.

The RootNodeTemplate and RootNodeStyle properties are applied to the node that represents the root of the site navigation hierarchy. If a NodeTemplate is defined in addition to the RootNodeTemplate, it is ignored. If a NodeStyle is defined in addition to the RootNodeStyle, it is merged with the RootNodeStyle to create a merged style. This merged style uses all the elements of the RootNodeStyle, plus any additional elements of the NodeStyle that did not conflict with the CurrentNodeStyle. Finally, if the currently displayed page is the root page of the site, the RootNodeTemplate and RootNodeStyle are used instead of the CurrentNodeTemplate or CurrentNodeStyle.

The SiteMapPath control uses the site map provider identified by the SiteMapProvider property as its data source for site navigation information. If no provider is specified, it uses the default provider for the site, identified in the SiteMap.Provider property. Typically, this is an instance of the default site map provider for ASP.NET, the XmlSiteMapProvider. If the SiteMapPath control is used within a site but no site map provider is configured, the control throws an HttpException exception.

The SiteMapPath control also provides events that you can program against. This allows you to run a custom routine whenever an event occurs. The following table lists the events supported by the SiteMapPath control.

Event

Description

ItemCreated

Occurs when the SiteMapPath control first creates a SiteMapNodeItem and associates it with a SiteMapNode.

ItemDataBound

Occurs when a SiteMapNodeItem is bound to site map data contained by the SiteMapNode.

Classes that derive from SiteMapPath override the InitializeItem method to customize the SiteMapNodeItem controls contained by the navigation control. For complete control over the way SiteMapNodeItem objects are created and added to the SiteMapPath, derived classes override the CreateControlHierarchy method.

Accessibility

The markup rendered by default for this control might not conform to accessibility standards such as the Web Content Accessibility Guidelines 1.0 (WCAG) priority 1 guidelines. For details about accessibility support for this control, see ASP.NET Controls and Accessibility.

TopicLocation
How to: Programmatically Enumerate Site-Map NodesBuilding ASP .NET Web Applications
How to: Configure Multiple Site Maps and Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server ControlsBuilding ASP .NET Web Applications
How to: Programmatically Modify Site-Map Nodes in MemoryBuilding ASP .NET Web Applications
How to: Localize Site-Map DataBuilding ASP .NET Web Applications
How to: Customize the Appearance of SiteMapPath Web Server ControlsBuilding ASP .NET Web Applications
How to: Display Site-Map Data in Non-Hierarchical Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications
How to: Implement ASP.NET Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Programmatically Enumerate Site-Map NodesBuilding ASP .NET Web Applications
How to: Configure Multiple Site Maps and Site-Map ProvidersBuilding ASP .NET Web Applications
How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server ControlsBuilding ASP .NET Web Applications
How to: Programmatically Modify Site-Map Nodes in MemoryBuilding ASP .NET Web Applications
How to: Localize Site-Map DataBuilding ASP .NET Web Applications
How to: Customize the Appearance of SiteMapPath Web Server ControlsBuilding ASP .NET Web Applications
How to: Display Site-Map Data in Non-Hierarchical Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications
How to: Implement ASP.NET Site-Map ProvidersBuilding ASP .NET Web Applications
Walkthrough: Adding Site Navigation to a Web SiteBuilding ASP .NET Web Applications in Visual Studio
How to: Add Simple Site NavigationBuilding ASP .NET Web Applications in Visual Studio

The following code example uses a SiteMapPath control declaratively in a Web Forms page. This example demonstrates some of the rules of precedence that govern the order with which templates and styles are applied to SiteMapPath nodes.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
        <form id="form1" runat="server">

            <!-- The following example demonstrates some of the orders
                 of precedence when applying styles and templates to
                 functional nodes of a SiteMapPath.

                 The NodeStyle and RootNodeStyle define the same attributes,
                 but are different and conflict with each other: the
                 RootNodeStyle supersedes NodeStyle, and is the style
                 rendered. Notice, however, that the underline style
                 defined by NodeStyle is still applied.

                 Both a CurrentNodeStyle and a CurrentNodeTemplate are
                 defined. A template supersedes a style for a node
                 type, so CurrentNodeTemplate is displayed and CurrentNodeStyle
                 is ignored. -->

            <asp:SiteMapPath ID="SiteMapPath1" runat="server"
                RenderCurrentNodeAsLink="true"
                NodeStyle-Font-Names="Franklin Gothic Medium"
                NodeStyle-Font-Underline="true"
                NodeStyle-Font-Bold="true"
                RootNodeStyle-Font-Names="Symbol"
                RootNodeStyle-Font-Bold="false"
                CurrentNodeStyle-Font-Names="Verdana"
                CurrentNodeStyle-Font-Size="10pt"
                CurrentNodeStyle-Font-Bold="true"
                CurrentNodeStyle-ForeColor="red"
                CurrentNodeStyle-Font-Underline="false">
                <CURRENTNODETEMPLATE>
                        <asp:Image id="Image1" runat="server" ImageUrl="WebForm2.jpg" AlternateText="WebForm2"/>
                </CURRENTNODETEMPLATE>
            </asp:SiteMapPath>


        </form>
    </body>
</html>

The previous example uses the default site map provider and a Web.sitemap file with the following structure.

<siteMap>
  <siteMapNode title="WebForm1" description="WebForm1" url="WebForm1.aspx" >
    <siteMapNode title="WebForm2" description="WebForm2" url="WebForm2.aspx"/>
  </siteMapNode>
</siteMap>

The following code example demonstrates extends the SiteMapPath control and adds new functionality to it by overriding the InitializeItem method. The DropDownSiteMapPath control adds a DropDownList after the current node, to enable easy navigation to pages that are child nodes of the current page. This example demonstrates how to work with SiteMapNodeItem objects, including checking their SiteMapNodeItemType and calling the OnItemCreated method after the items are created.

using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


// The DropDownNavigationPath is a class that extends the SiteMapPath 
// control and renders a DropDownList after the CurrentNode. The 
// DropDownList displays a list of pages found further down the site map 
// hierarchy from the current one. Selecting an item in the DropDownList 
// redirects to that page. 
// 
// For simplicity, the DropDownNavigationPath assumes the 
// RootToCurrent PathDirection, and does not apply styles 
// or templates the current node. 
//
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public class DropDownNavigationPath : SiteMapPath {
    // Override the InitializeItem method to add a PathSeparator 
    // and DropDownList to the current node. 
    protected override void InitializeItem(SiteMapNodeItem item) {

        // The only node that must be handled is the CurrentNode. 
        if (item.ItemType == SiteMapNodeItemType.Current)
        {
            HyperLink hLink = new HyperLink();

            // No Theming for the HyperLink.
            hLink.EnableTheming = false;
            // Enable the link of the SiteMapPath is enabled.
            hLink.Enabled = this.Enabled;

            // Set the properties of the HyperLink to 
            // match those of the corresponding SiteMapNode.
            hLink.NavigateUrl = item.SiteMapNode.Url;
            hLink.Text        = item.SiteMapNode.Title;
            if (ShowToolTips) {
                hLink.ToolTip = item.SiteMapNode.Description;
            }

            // Apply styles or templates to the HyperLink here. 
            // ... 
            // ... 

            // Add the item to the Controls collection.
            item.Controls.Add(hLink);

            AddDropDownListAfterCurrentNode(item);
        }
        else {
            base.InitializeItem(item);
        }
    }
    private void AddDropDownListAfterCurrentNode(SiteMapNodeItem item) {

        SiteMapNodeCollection childNodes = item.SiteMapNode.ChildNodes;

        // Only do this work if there are child nodes. 
        if (childNodes != null) {

            // Add another PathSeparator after the CurrentNode.
            SiteMapNodeItem finalSeparator =
                new SiteMapNodeItem(item.ItemIndex,
                                    SiteMapNodeItemType.PathSeparator);

            SiteMapNodeItemEventArgs eventArgs =
                new SiteMapNodeItemEventArgs(finalSeparator);

            InitializeItem(finalSeparator);
            // Call OnItemCreated every time a SiteMapNodeItem is 
            // created and initialized.
            OnItemCreated(eventArgs);

            // The pathSeparator does not bind to any SiteMapNode, so 
            // do not call DataBind on the SiteMapNodeItem.
            item.Controls.Add(finalSeparator);

            // Create a DropDownList and populate it with the children of the 
            // CurrentNode. There are no styles or templates that are applied 
            // to the DropDownList control. If OnSelectedIndexChanged is raised, 
            // the event handler redirects to the page selected. 
            // The CurrentNode has child nodes.
            DropDownList ddList = new DropDownList();
            ddList.AutoPostBack = true;

            ddList.SelectedIndexChanged += new EventHandler(this.DropDownNavPathEventHandler);

            // Add a ListItem to the DropDownList for every node in the 
            // SiteMapNodes collection. 
            foreach (SiteMapNode node in childNodes) {
                ddList.Items.Add(new ListItem(node.Title, node.Url));
            }

            item.Controls.Add(ddList);
        }
    }

    // The sender is the DropDownList. 
    private void DropDownNavPathEventHandler(object sender,EventArgs e) {
        DropDownList ddL = sender as DropDownList;

        // Redirect to the page the user chose. 
        if (Context != null)
            Context.Response.Redirect(ddL.SelectedValue);
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft