這幾天閑來無事,就在園子裡找前輩們關於ASP.NET網站導覽的東西,找了好半天,不過頗有收穫,呵呵!但是我想先把我自己看到的關於ASP.NET網站導覽控制項的知識摘抄下來,以備後用:
SiteMapPath:顯示一組文本或映像超連結,使您可以在使用最少頁面空間的同時更輕鬆地定位網站。
SiteMapPath 控制項是一種網站導覽控制項,反映 SiteMap 對象提供的資料。它提供了一種用於輕鬆定位網站的節省空間的方式,用作當前顯示頁在網站中位置的引用點。此種類型的控制項通常稱為麵包屑或眉毛,因為它顯示了超連結頁名稱的分層路徑,從而提供了從當前位置沿頁階層向上的跳轉。SiteMapDataSource。SiteMapPath 對於分層頁結構較深的網站很有用,在此類網站中 TreeView 或 Menu 可能需要較多的頁空間。
SiteMapPath 控制項直接使用網站的網站地圖資料。如果將其用在未在網站地圖中表示的頁面上,則其不會顯示。有關網站地圖的更多資訊,請參見。
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 控制項支援的事件。
事件 |
說明 |
ItemCreated |
SiteMapPath 控制項先建立一個 SiteMapNodeItem,然後將其與 SiteMapNode 關聯時發生。 |
ItemDataBound |
將 SiteMapNodeItem 綁定到 SiteMapNode 包含的網站地圖資料時發生。 |
派生自 SiteMapPath 的類會重寫 InitializeItem 方法,以自訂導航控制項包含的 SiteMapNodeItem 控制項。為了完全控制 SiteMapNodeItem 對象的建立方式以及將其添加到 SiteMapPath 的方式,衍生類別會重寫 CreateControlHierarchy 方法。
下面的程式碼範例示範如何通過重寫 InitializeItem 方法,擴充 SiteMapPath 控制項並向其添加新功能。DropDownSiteMapPath 控制項在當前節點後添加一個 DropDownList,使得定位到當前頁的子節點頁面變得容易。此樣本示範如何在建立項後使用 SiteMapNodeItem 對象,包括檢查它們的 SiteMapNodeItemType 及調用 OnItemCreated 方法。
Code
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);
}
}
Web.sitemap和Web.config中的Role節點:
1、只有角色為2的使用者才能看到這個節點
<siteMapNode title="Parent1_Child1" url="~/Parent1/Parent1_Child1.aspx" roles="2"/>
2、只有角色為1或2的使用者才能操作這個檔案夾中的檔案
<location path="Parent2">
<system.web>
<authorization>
<allow roles="1,2" />
<deny users="*" />
</authorization>
</system.web>
</location>
比如,當會員登陸後,他的個人介面有一個菜單,菜單中有所有可以看到的功能,但是有些功能他必須付費後才能使用,那麼就必須設定roles屬性了。