注意:此类在 .NET Framework 2.0 版中是新增的。
显示一组文本或图像超链接,使您可以在使用最少页面空间的同时更轻松地定位网站。
命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)
Public Class SiteMapPath
Inherits CompositeControl
Dim instance As SiteMapPath
public class SiteMapPath : CompositeControl
public ref class SiteMapPath : public CompositeControl
public class SiteMapPath extends CompositeControl
public class SiteMapPath extends CompositeControl
SiteMapPath 控件是一种站点导航控件,反映 SiteMap 对象提供的数据。它提供了一种用于轻松定位站点的节省空间方式,用作当前显示页在站点中位置的引用点。此种类型的控件通常称为面包屑或眉毛,因为它显示了超链接页名称的分层路径,从而提供了从当前位置沿页层次结构向上的跳转。SiteMapDataSource。SiteMapPath 对于分层页结构较深的站点很有用,在此类站点中 TreeView 或 Menu 可能需要较多的页空间。
SiteMapPath 控件直接使用网站的站点地图数据。如果将其用在未在站点地图中表示的页面上,则其不会显示。有关站点地图的更多信息,请参见 ASP.NET 站点导航概述。
SiteMapPath 由节点组成。路径中的每个元素均称为节点,用 SiteMapNodeItem 对象表示。锚定路径并表示分层树的根的节点称为根节点。表示当前显示页的节点称为当前节点。当前节点与根节点之间的任何其他节点都为父节点。下表描述了三种不同的节点类型。
| 节点类型 | 说明 |
| 根节点 | 锚定节点分层组的节点。 |
| 父节点 | 有一个或多个子节点但不是当前节点的节点。 |
| 当前节点 | 表示当前显示页的节点。 |
SiteMapPath 显示的每个节点都是 HyperLink 或 Literal 控件,您可以将模板或样式应用到这两种控件。对节点应用模板和样式需遵循两个优先级规则:
NodeStyle 和 NodeTemplate 属性适用于所有节点,而不考虑节点类型。如果同时定义了这两个属性,将优先使用 NodeTemplate。
CurrentNodeTemplate 和 CurrentNodeStyle 属性适用于表示当前显示页的节点。如果除了 CurrentNodeTemplate 外,还定义了 NodeTemplate,则将忽略它。如果除了 CurrentNodeStyle 外,还定义了 NodeStyle,则它将与 CurrentNodeStyle 合并,从而创建合并样式。此合并样式使用 CurrentNodeStyle 的所有元素,以及 NodeStyle 中不与 CurrentNodeStyle 冲突的任何附加元素。
RootNodeTemplate 和 RootNodeStyle 属性适用于表示站点导航层次结构根的节点。如果除了 RootNodeTemplate 外,还定义了 NodeTemplate,则将忽略它。如果除了 RootNodeStyle 外,还定义了 NodeStyle,则它将与 RootNodeStyle 合并,从而创建合并样式。此合并样式使用 RootNodeStyle 的所有元素,以及 NodeStyle 中不与 CurrentNodeStyle 冲突的任何附加元素。最后,如果当前显示页是该站点的根页,将使用 RootNodeTemplate 和 RootNodeStyle,而不是 CurrentNodeTemplate 或 CurrentNodeStyle。
SiteMapPath 控件将由 SiteMapProvider 属性标识的站点地图提供程序用作站点导航信息的数据源。如果未指定提供程序,它将使用站点的默认提供程序,此提供程序由 SiteMap.Provider 属性标识。通常,这是 ASP.NET 默认站点地图提供程序(即 XmlSiteMapProvider)的一个实例。如果在站点内使用了 SiteMapPath 控件,但未配置站点地图提供程序,该控件将引发 HttpException 异常。
SiteMapPath 控件还提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了 SiteMapPath 控件支持的事件。
派生自 SiteMapPath 的类会重写 InitializeItem 方法,以自定义导航控件包含的 SiteMapNodeItem 控件。为了完全控制 SiteMapNodeItem 对象的创建方式以及将其添加到 SiteMapPath 的方式,派生类会重写 CreateControlHierarchy 方法。
辅助功能
默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能。
下面的代码示例在 Web 窗体页中以声明方式使用了 SiteMapPath 控件。此示例演示一些优先级规则,这些规则控制了将模板和样式应用到 SiteMapPath 节点的顺序。
<%@ Page Language="VB" %>
<!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>
<BODY>
<FORM 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-Name="Franklin Gothic Medium"
NodeStyle-Font-Underline="true"
NodeStyle-Font-Bold="true"
RootNodeStyle-Font-Name="Symbol"
RootNodeStyle-Font-Bold="false"
CurrentNodeStyle-Font-Name="Verdana"
CurrentNodeStyle-Font-Size="10pt"
CurrentNodeStyle-Font-Bold="true"
CurrentNodeStyle-ForeColor="red"
CurrentNodeStyle-Font-Underline="false"
HoverNodeStyle-ForeColor="blue"
HoverNodeStyle-Font-Underline="true">
<CURRENTNODETEMPLATE>
<asp:Image id="Image1" runat="server" ImageUrl="WebForm2.jpg" AlternateText="WebForm2"/>
</CURRENTNODETEMPLATE>
</asp:SiteMapPath>
</FORM>
</BODY>
</HTML>
<%@ 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>
<BODY>
<FORM 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-Name="Franklin Gothic Medium"
NodeStyle-Font-Underline="true"
NodeStyle-Font-Bold="true"
RootNodeStyle-Font-Name="Symbol"
RootNodeStyle-Font-Bold="false"
CurrentNodeStyle-Font-Name="Verdana"
CurrentNodeStyle-Font-Size="10pt"
CurrentNodeStyle-Font-Bold="true"
CurrentNodeStyle-ForeColor="red"
CurrentNodeStyle-Font-Underline="false"
HoverNodeStyle-ForeColor="blue"
HoverNodeStyle-Font-Underline="true">
<CURRENTNODETEMPLATE>
<asp:Image id="Image1" runat="server" ImageUrl="WebForm2.jpg" AlternateText="WebForm2"/>
</CURRENTNODETEMPLATE>
</asp:SiteMapPath>
</FORM>
</BODY>
</HTML>
<%@ Page Language="VJ#" %>
<!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>
<BODY>
<FORM 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 supercedes 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 supercedes a style for a node
type, so CurrentNodeTemplate is displayed and CurrentNodeStyle
is ignored. -->
<asp:SiteMapPath ID="SiteMapPath1" runat="server"
RenderCurrentNodeAsLink="true"
NodeStyle-Font-Name="Franklin Gothic Medium"
NodeStyle-Font-Underline="true"
NodeStyle-Font-Bold="true"
RootNodeStyle-Font-Name="Symbol"
RootNodeStyle-Font-Bold="false"
CurrentNodeStyle-Font-Name="Verdana"
CurrentNodeStyle-Font-Size="10pt"
CurrentNodeStyle-Font-Bold="true"
CurrentNodeStyle-ForeColor="red"
CurrentNodeStyle-Font-Underline="false"
HoverNodeStyle-ForeColor="blue"
HoverNodeStyle-Font-Underline="true">
<CURRENTNODETEMPLATE>
<asp:Image id="Image1" runat="server" ImageUrl="WebForm2.jpg" AlternateText="WebForm2"/>
</CURRENTNODETEMPLATE>
</asp:SiteMapPath>
</FORM>
</BODY>
</HTML>
上一个示例使用了默认的站点地图提供程序,以及具有如下结构的 Web.sitemap 文件。
<siteMap>
<siteMapNode title="WebForm1" description="WebForm1" url="WebForm1.aspx" >
<siteMapNode title="WebForm2" description="WebForm2" url="WebForm2.aspx"/>
</siteMapNode>
</siteMap>
下面的代码示例演示如何通过重写 InitializeItem 方法,扩展 SiteMapPath 控件并向其添加新功能。DropDownSiteMapPath 控件在当前节点后添加一个 DropDownList,使得定位到当前页的子节点页面变得容易。此示例演示如何在创建项后使用 SiteMapNodeItem 对象,包括检查它们的 SiteMapNodeItemType 及调用 OnItemCreated 方法。
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace Samples.AspNet
' 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
Inherits SiteMapPath
' Override the InitializeItem method to add a PathSeparator
' and DropDownList to the current node.
Protected Overrides Sub InitializeItem(item As SiteMapNodeItem)
' The only node that must be handled is the CurrentNode.
If item.ItemType = SiteMapNodeItemType.Current Then
Dim hLink As New HyperLink()
' No Theming for the HyperLink.
hLink.EnableTheming = False
' Enable the link of the SiteMapPath is enabled.
hLink.Enabled = Me.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 Then
hLink.ToolTip = item.SiteMapNode.Description
End If
' Apply styles or templates to the HyperLink here.
' ...
' ...
' Add the item to the Controls collection.
item.Controls.Add(hLink)
AddDropDownListAfterCurrentNode(item)
Else
MyBase.InitializeItem(item)
End If
End Sub 'InitializeItem
Private Sub AddDropDownListAfterCurrentNode(item As SiteMapNodeItem)
Dim childNodes As SiteMapNodeCollection = item.SiteMapNode.ChildNodes
' Only do this work if there are child nodes.
If Not (childNodes Is Nothing) Then
' Add another PathSeparator after the CurrentNode.
Dim finalSeparator As New SiteMapNodeItem(item.ItemIndex, SiteMapNodeItemType.PathSeparator)
Dim eventArgs As 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.
Dim ddList As New DropDownList()
ddList.AutoPostBack = True
AddHandler ddList.SelectedIndexChanged, AddressOf Me.DropDownNavPathEventHandler
' Add a ListItem to the DropDownList for every node in the
' SiteMapNodes collection.
Dim node As SiteMapNode
For Each node In childNodes
ddList.Items.Add(New ListItem(node.Title, node.Url))
Next node
item.Controls.Add(ddList)
End If
End Sub 'AddDropDownListAfterCurrentNode
' The sender is the DropDownList.
Private Sub DropDownNavPathEventHandler(sender As Object, e As EventArgs)
Dim ddL As DropDownList = CType(sender, DropDownList)
' Redirect to the page the user chose.
If Not (Context Is Nothing) Then
Context.Response.Redirect(ddL.SelectedValue)
End If
End Sub 'DropDownNavPathEventHandler
End Class 'DropDownNavigationPath
End Namespace
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);
}
}
import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Web.*;
import System.Web.UI.*;
import 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.
//
public class DropDownNavigationPath extends SiteMapPath
{
// Override the InitializeItem method to add a PathSeparator
// and DropDownList to the current node.
protected void InitializeItem(SiteMapNodeItem item)
{
// The only node that must be handled is the CurrentNode.
if (item.get_ItemType().Equals(SiteMapNodeItemType.Current)) {
HyperLink hLink = new HyperLink();
// No Theming for the HyperLink.
hLink.set_EnableTheming(false);
// Enable the link of the SiteMapPath is enabled.
hLink.set_Enabled(this.get_Enabled());
// Set the properties of the HyperLink to
// match those of the corresponding SiteMapNode.
hLink.set_NavigateUrl(item.get_SiteMapNode().get_Url());
hLink.set_Text(item.get_SiteMapNode().get_Title());
if (get_ShowToolTips()) {
hLink.set_ToolTip(item.get_SiteMapNode().get_Description());
}
// Apply styles or templates to the HyperLink here.
// ...
// ...
// Add the item to the Controls collection.
item.get_Controls().Add(hLink);
AddDropDownListAfterCurrentNode(item);
}
else {
super.InitializeItem(item);
}
} //InitializeItem
private void AddDropDownListAfterCurrentNode(SiteMapNodeItem item)
{
SiteMapNodeCollection childNodes = item.get_SiteMapNode().
get_ChildNodes();
// Only do this work if there are child nodes.
if (childNodes != null) {
// Add another PathSeparator after the CurrentNode.
SiteMapNodeItem finalSeparator = new SiteMapNodeItem(item.
get_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.get_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.set_AutoPostBack(true);
ddList.add_SelectedIndexChanged(new EventHandler(this.
DropDownNavPathEventHandler));
// Add a ListItem to the DropDownList for every node in the
// SiteMapNodes collection.
for (int iCtr = 0; iCtr < childNodes.get_Count(); iCtr++) {
SiteMapNode node = (SiteMapNode)childNodes.get_Item(iCtr);
ddList.get_Items().Add(new ListItem(node.get_Title(), node.
get_Url()));
}
item.get_Controls().Add(ddList);
}
} //AddDropDownListAfterCurrentNode
// The sender is the DropDownList.
public void DropDownNavPathEventHandler(Object sender, EventArgs e)
{
DropDownList ddL = (DropDownList)sender;
// Redirect to the page the user chose.
if (get_Context() != null) {
get_Context().get_Response().Redirect(ddL.get_SelectedValue());
}
} //DropDownNavPathEventHandler
} //DropDownNavigationPath
System.Object
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.CompositeControl
System.Web.UI.WebControls.SiteMapPath
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0