SiteMapNode 類別

定義

代表階層式網站導覽結構中的節點 (例如 SiteMap 類別以及實作抽象 SiteMapProvider 類別所述的節點)。

public ref class SiteMapNode : ICloneable, System::Web::UI::IHierarchyData, System::Web::UI::INavigateUIData
public class SiteMapNode : ICloneable, System.Web.UI.IHierarchyData, System.Web.UI.INavigateUIData
type SiteMapNode = class
    interface ICloneable
    interface IHierarchyData
    interface INavigateUIData
Public Class SiteMapNode
Implements ICloneable, IHierarchyData, INavigateUIData
繼承
SiteMapNode
實作

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何建立新的網站地圖節點集合,並將元素新增至其中。 第二個程式碼範例示範如何從文字檔載入網站地圖資料。

下列程式碼範例示範如何使用 SiteMapNodeCollection 建構函式來建立新的 SiteMapNodeCollection 集合,然後使用 方法將元素加入其中 Add

// The LoadSiteMapData() method loads site navigation
// data from persistent storage into a DataTable.
DataTable siteMap = LoadSiteMapData();

// Create a SiteMapNodeCollection.
SiteMapNodeCollection nodes = new SiteMapNodeCollection();

// Create a SiteMapNode and add it to the collection.
SiteMapNode tempNode;
DataRow row;
int index = 0;

while (index < siteMap.Rows.Count)
{

    row = siteMap.Rows[index];

    // Create a node based on the data in the DataRow.
    tempNode = new SiteMapNode(SiteMap.Provider,
                                row["Key"].ToString(),
                                row["Url"].ToString());

    // Add the node to the collection.
    nodes.Add(tempNode);
    ++index;
}
' The LoadSiteMapData() Function loads site navigation
' data from persistent storage into a DataTable.

Dim siteMapData As DataTable
siteMapData = LoadSiteMapData()

' Create a SiteMapNodeCollection.
Dim nodes As New SiteMapNodeCollection()

' Create a SiteMapNode and add it to the collection.
Dim tempNode As SiteMapNode
Dim row As DataRow
Dim index As Integer
index = 0

While (index < siteMapData.Rows.Count)

    row = siteMapData.Rows(index)

    ' Create a node based on the data in the DataRow.
    tempNode = New SiteMapNode(SiteMap.Provider, row("Key").ToString(), row("Url").ToString())

    ' Add the node to the collection.
    nodes.Add(tempNode)
    index = index + 1
End While

下列程式碼範例示範 如何 SimpleTextSiteMapProvider 剖析文字檔,其中包含逗號分隔字串中的網站地圖資料。 系統會針對從檔案讀取的每一行,將新的 SiteMapNode 物件新增至 類別的內部追蹤集合。

此程式碼範例是提供給 類別之較大範例的 SiteMapProvider 一部分。

protected virtual void LoadSiteMapFromStore()
{
  string pathToOpen;

  lock (this)
  {
    // If a root node exists, LoadSiteMapFromStore has already
    // been called, and the method can return.
    if (rootNode != null)
    {
      return;
    }
    else
    {
      pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + sourceFilename);

      if (File.Exists(pathToOpen))
      {
        // Open the file to read from.
        using (StreamReader sr = File.OpenText(pathToOpen))
        {

          // Clear the state of the collections and rootNode
          rootNode = null;
          siteMapNodes.Clear();
          childParentRelationship.Clear();

          // Parse the file and build the site map
          string s = "";
          string[] nodeValues = null;
          SiteMapNode temp = null;

          while ((s = sr.ReadLine()) != null)
          {

            // Build the various SiteMapNode objects and add
            // them to the ArrayList collections. The format used
            // is: URL,TITLE,DESCRIPTION,PARENTURL

            nodeValues = s.Split(',');

            temp = new SiteMapNode(this,
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                nodeValues[1],
                nodeValues[2]);

            // Is this a root node yet?
            if (null == rootNode &&
                string.IsNullOrEmpty(nodeValues[3]))
            {
              rootNode = temp;
            }

          // If not the root node, add the node to the various collections.
            else
            {
              siteMapNodes.Add(new DictionaryEntry(temp.Url, temp));
              // The parent node has already been added to the collection.
              SiteMapNode parentNode =
                       FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
              if (parentNode != null)
              {
                childParentRelationship.Add(new DictionaryEntry(temp.Url, parentNode));
              }
              else
              {
                throw new Exception("Parent node not found for current node.");
              }
            }
          }
        }
      }
      else
      {
        throw new Exception("File not found");
      }
    }
  }
  return;
}
  Protected Overridable Sub LoadSiteMapFromStore()
    Dim pathToOpen As String
    SyncLock Me
      ' If a root node exists, LoadSiteMapFromStore has already
      ' been called, and the method can return.
      If Not (aRootNode Is Nothing) Then
        Return
      Else
        pathToOpen = HttpContext.Current.Server.MapPath("~" & "\\" & sourceFilename)
        If File.Exists(pathToOpen) Then
          ' Open the file to read from.
          Dim sr As StreamReader = File.OpenText(pathToOpen)
          Try

            ' Clear the state of the collections and aRootNode
            aRootNode = Nothing
            siteMapNodes.Clear()
            childParentRelationship.Clear()

            ' Parse the file and build the site map
            Dim s As String = ""
            Dim nodeValues As String() = Nothing
            Dim temp As SiteMapNode = Nothing

            Do
              s = sr.ReadLine()

              If Not s Is Nothing Then
                ' Build the various SiteMapNode objects and add
                ' them to the ArrayList collections. The format used
                ' is: URL,TITLE,DESCRIPTION,PARENTURL
                nodeValues = s.Split(","c)

                temp = New SiteMapNode(Me, _
                    HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                    HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                    nodeValues(1), _
                    nodeValues(2))

                ' Is this a root node yet?
                If aRootNode Is Nothing AndAlso _
                  (nodeValues(3) Is Nothing OrElse _
                   nodeValues(3) = String.Empty) Then
                  aRootNode = temp

                  ' If not the root node, add the node to the various collections.
                Else

                  siteMapNodes.Add(New DictionaryEntry(temp.Url, temp))

                  ' The parent node has already been added to the collection.
                  Dim parentNode As SiteMapNode = _
                      FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(3))

                  If Not (parentNode Is Nothing) Then
                    childParentRelationship.Add(New DictionaryEntry(temp.Url, parentNode))
                  Else
                    Throw New Exception("Parent node not found for current node.")
                  End If
                End If
              End If
            Loop Until s Is Nothing
          Finally
            sr.Close()
          End Try
        Else
          Throw New Exception("File not found")
        End If
      End If
    End SyncLock
    Return
  End Sub
End Class

備註

SiteMapNode物件代表網站地圖結構中的網站頁面。 SiteMapNode 物件是由靜態 SiteMap 類別在執行時間使用一或多個網站地圖提供者載入,以將網站地圖資料從永續性儲存體載入記憶體中。 SiteMapNode 物件會由 SiteMapNodeItem 類別包裝,以供 Web 服務器控制項使用,例如 SiteMapPath 控制項。

類別 SiteMapNode 包含數個用來描述網站中單一頁面的屬性,包括描述頁面的屬性,例如 UrlTitleDescription 屬性。 Url雖然 屬性是由 XmlSiteMapProvider 類別使用,這是 ASP.NET 的預設網站地圖提供者,做為提供者用來追蹤節點的內部集合中的查閱索引鍵,但 類別 SiteMapNode 支援基本 Key 屬性,可供網站地圖提供者用來追蹤節點。 此外, Url 導覽控制項會使用 屬性來呈現導覽結構內頁面的超連結。 屬性 Title 是 的易記名稱 SiteMapNode ,通常與 Web 表單的 HTML 標題相同,而且由導覽控制項用來轉譯簡單標籤。 最後, NameValueCollection 其他 Attributes 屬性的集合可供使用物件的網站地圖提供者使用 SiteMapNode ,但需要基 SiteMapNode 類中無法使用的其他屬性。

建構函式

SiteMapNode(SiteMapProvider, String)

使用識別節點所表示之網頁的指定 key 以及管理節點的網站導覽提供者,初始化 SiteMapNode 類別的新執行個體。

SiteMapNode(SiteMapProvider, String, String)

使用指定的 URL、識別節點所表示之網頁的 key,以及管理節點的網站導覽提供者,初始化 SiteMapNode 類別的新執行個體。

SiteMapNode(SiteMapProvider, String, String, String)

使用指定的 URL、識別節點所表示之網頁的 key、標題以及管理節點的網站導覽提供者,初始化 SiteMapNode 類別的新執行個體。

SiteMapNode(SiteMapProvider, String, String, String, String)

使用指定的 URL、識別節點所表示之網頁的 key、標題和描述,以及管理節點的網站導覽提供者,初始化 SiteMapNode 類別的新執行個體。

SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String)

使用管理節點、URL、標題、描述、角色、其他屬性,以及當地語系化的明確和隱含資源索引鍵之指定網站導覽提供者,初始化 SiteMapNode 類別的新執行個體。

屬性

Attributes

取得或設定為 SiteMapNode 類別所定義之強型別屬性 (Property) 以外的其他屬性 (Attribute) 集合。

ChildNodes

從關聯的 SiteMapNode 提供者,取得或設定目前 SiteMapProvider 物件的所有子節點。

Description

取得或設定 SiteMapNode 的描述。

HasChildNodes

取得值,指出目前 SiteMapNode 是否有任何子節點。

Item[String]

根據指定的索引鍵,取得或設定 Attributes 集合的自訂屬性或資源字串。

Key

取得字串,表示網站導覽節點的查閱索引鍵。

NextSibling

取得與目前節點在相同階層式層級的下一個 SiteMapNode 節點,相對於 ParentNode 屬性 (如果有的話)。

ParentNode

取得或設定目前節點之父代的 SiteMapNode 物件。

PreviousSibling

取得與目前節點在相同層級的上一個 SiteMapNode 節點,相對於 ParentNode 物件 (如果有的話)。

Provider

取得用來追蹤 SiteMapProvider 物件的 SiteMapNode 提供者。

ReadOnly

取得或設定值,指出網站導覽節點是否可以修改。

ResourceKey

取得或設定用來當地語系化 SiteMapNode 的資源索引鍵。

Roles

取得與 SiteMapNode 物件相關聯的角色集合 (用於安全性調整期間)。

RootNode

取得網站導覽提供者階層架構中根提供者的根節點。 如果提供者階層架構不存在,RootNode 屬性會取得目前提供者的根節點。

Title

取得或設定 SiteMapNode 物件的標題。

Url

取得或設定 SiteMapNode 物件所表示之網頁的 URL。

方法

Clone()

建立目前節點複本的新節點。

Clone(Boolean)

建立目前節點複本的新節點,並選擇性複製目前節點的所有父代和祖系節點。

Equals(Object)

取得值,指出目前的 SiteMapNode 是否與指定的物件相同。

GetAllNodes()

擷取呼叫節點之所有子代 SiteMapNode 物件的唯讀集合,不論分隔程度為何。

GetDataSourceView(SiteMapDataSource, String)

擷取與目前節點關聯的 SiteMapDataSourceView 物件。

GetExplicitResourceString(String, String, Boolean)

根據要當地語系化的 SiteMapNode 屬性,擷取當地語系化字串、找不到資源時所傳回的預設字串,以及指出找不到資源時是否擲回例外狀況之布林值。

GetHashCode()

傳回 SiteMapNode 物件的雜湊程式碼。

GetHierarchicalDataSourceView()

擷取與目前節點關聯的 SiteMapHierarchicalDataSourceView 物件。

GetImplicitResourceString(String)

根據屬性 (Attribute) 名稱和用來追蹤 ResourceKeySiteMapProvider 所指定之 SiteMapNode 屬性 (Property),取得當地語系化字串。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsAccessibleToUser(HttpContext)

取得值,指出在指定內容中使用者是否可以檢視指定的網站導覽節點。

IsDescendantOf(SiteMapNode)

取得值,指出目前網站導覽節點是指定節點的子系或直接子代。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

SiteMapNode 類別這個執行個體的值轉換成它的對等字串表示。

明確介面實作

ICloneable.Clone()

建立目前節點複本的新節點。 如需這個成員的說明,請參閱 Clone()

IHierarchyData.GetChildren()

擷取目前項目的階層式子系資料項目。 如需這個成員的說明,請參閱 GetChildren()

IHierarchyData.GetParent()

擷取目前項目的階層式父代。 如需這個成員的說明,請參閱 GetParent()

IHierarchyData.HasChildren

取得指出目前的 SiteMapNode 物件是否有任何子節點的值。 如需這個成員的說明,請參閱 HasChildren

IHierarchyData.Item

取得階層式資料項目。 如需這個成員的說明,請參閱 Item

IHierarchyData.Path

取得階層式資料項目的路徑。 如需這個成員的說明,請參閱 Path

IHierarchyData.Type

取得字串,表示階層式資料項目的型別名稱。 如需這個成員的說明,請參閱 Type

INavigateUIData.Description

取得網站地圖節點的 Description 屬性。 如需這個成員的說明,請參閱 Description

INavigateUIData.Name

取得網站地圖節點的 Title 屬性。 如需這個成員的說明,請參閱 Name

INavigateUIData.NavigateUrl

取得網站地圖節點的 Url 屬性。 如需這個成員的說明,請參閱 NavigateUrl

INavigateUIData.Value

取得網站地圖節點的 Title 屬性。 如需這個成員的說明,請參閱 Value

適用於

另請參閱