SiteMapNode 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化該類別的新實例 SiteMapNode ,並將其與指定的 SiteMapProvider 物件關聯。
多載
| 名稱 | Description |
|---|---|
| SiteMapNode(SiteMapProvider, String) |
初始化該類別的新實例 SiteMapNode ,利用指定的 |
| SiteMapNode(SiteMapProvider, String, String) |
初始化該類別的新實例 SiteMapNode ,使用指定的 URL、a |
| SiteMapNode(SiteMapProvider, String, String, String) |
初始化該類別的新實例 SiteMapNode ,使用指定的 URL、a |
| SiteMapNode(SiteMapProvider, String, String, String, String) |
初始化類別的新實例 SiteMapNode ,使用指定的 URL、a |
| SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String) |
使用指定的網站地圖提供者初始化該類別的新實例 SiteMapNode ,該服務提供者負責管理節點、URL、標題、描述、角色、額外屬性,以及用於本地化的顯式與隱式資源鍵。 |
SiteMapNode(SiteMapProvider, String)
初始化該類別的新實例 SiteMapNode ,利用指定的 key ID 來識別節點所代表的頁面及管理該節點的站點映射提供者。
public:
SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key);
public SiteMapNode(System.Web.SiteMapProvider provider, string key);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String)
參數
- provider
- SiteMapProvider
該節點所關聯的 。SiteMapProvider
- key
- String
一個提供者專屬的查詢金鑰。
例外狀況
備註
XmlSiteMapProvider該類別是 ASP.NET 的預設SiteMapProvider提供者實作,若節點有提供查詢鍵,則使用該SiteMapNode.Url屬性作為查詢鍵(若未提供 URL,則會產生該節點的追蹤識別碼)。 因此,任何 SiteMapNode 提供 URL 且由 使用者 XmlSiteMapProvider 使用的控制項,必須在提供者的範圍內擁有唯一的 URL。
適用於
SiteMapNode(SiteMapProvider, String, String)
初始化該類別的新實例 SiteMapNode ,使用指定的 URL、a key 來識別節點所代表的頁面,以及管理該節點的網站地圖提供者。
public:
SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url);
public SiteMapNode(System.Web.SiteMapProvider provider, string key, string url);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String)
參數
- provider
- SiteMapProvider
該節點所關聯的 。SiteMapProvider
- key
- String
一個提供者專屬的查詢金鑰。
- url
- String
節點在網站中所代表的頁面網址。
例外狀況
範例
以下程式碼範例示範如何使用 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
備註
XmlSiteMapProvider該類別是 ASP.NET 的預設SiteMapProvider提供者實作,若節點有提供查詢鍵,則使用該SiteMapNode.Url屬性作為查詢鍵(若未提供 URL,則會產生該節點的追蹤識別碼)。 因此,任何 SiteMapNode 提供 URL 且被 使用的 XmlSiteMapProvider 物件,必須在提供者範圍內擁有唯一的 URL。
適用於
SiteMapNode(SiteMapProvider, String, String, String)
初始化該類別的新實例 SiteMapNode ,使用指定的 URL、a key 來識別節點所代表的頁面、標題,以及管理該節點的網站地圖提供者。
public:
SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title);
public SiteMapNode(System.Web.SiteMapProvider provider, string key, string url, string title);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String)
參數
- provider
- SiteMapProvider
該節點所關聯的 。SiteMapProvider
- key
- String
一個提供者專屬的查詢金鑰。
- url
- String
節點在網站中所代表的頁面網址。
- title
- String
節點的標籤,通常由導航控制顯示。
例外狀況
範例
以下程式碼範例示範如何使用 SiteMapNode 建構子來建立該類別的新實例 SiteMapNode 。 節點初始化時會以 Microsoft Access 資料庫列的值。
此程式碼範例是該方法更大範例 BuildSiteMap 的一部分。
OleDbCommand^ rootNodeCommand = gcnew OleDbCommand
("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection);
OleDbDataReader^ rootNodeReader = rootNodeCommand->ExecuteReader();
if ( rootNodeReader->HasRows )
{
rootNodeReader->Read();
rootNodeId = rootNodeReader->GetInt32( 0 );
// Create a SiteMapNode that references the current StaticSiteMapProvider.
rootNode = gcnew SiteMapNode(this, rootNodeId.ToString(),
rootNodeReader->GetString( 1 ),rootNodeReader->GetString( 2 ));
}
else
return nullptr;
rootNodeReader->Close();
OleDbCommand rootNodeCommand =
new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
accessConnection);
OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();
if(rootNodeReader.HasRows) {
rootNodeReader.Read();
rootNodeId = rootNodeReader.GetInt32(0);
// Create a SiteMapNode that references the current StaticSiteMapProvider.
rootNode = new SiteMapNode(this,
rootNodeId.ToString(),
rootNodeReader.GetString(1),
rootNodeReader.GetString(2));
}
else
{
return null;
}
rootNodeReader.Close();
Dim rootNodeCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection)
Dim rootNodeReader As OleDbDataReader = rootNodeCommand.ExecuteReader()
If rootNodeReader.HasRows Then
rootNodeReader.Read()
rootNodeId = rootNodeReader.GetInt32(0)
' Create a SiteMapNode that references the current StaticSiteMapProvider.
aRootNode = New SiteMapNode(Me, rootNodeId.ToString(), rootNodeReader.GetString(1), rootNodeReader.GetString(2))
Else
Return Nothing
End If
rootNodeReader.Close()
備註
XmlSiteMapProvider該類別是 ASP.NET 的預設SiteMapProvider提供者實作,若節點有提供查詢鍵,則使用該SiteMapNode.Url屬性作為查詢鍵(若未提供 URL,則會產生該節點的追蹤識別碼)。 因此,任何 SiteMapNode 提供 URL 且被 使用的 XmlSiteMapProvider 物件,必須在提供者範圍內擁有唯一的 URL。
若提供 no title ,則呼叫 Title 該屬性回傳該 String.Empty 欄位。
適用於
SiteMapNode(SiteMapProvider, String, String, String, String)
初始化類別的新實例 SiteMapNode ,使用指定的 URL、a key 來識別該節點所代表的頁面、標題與描述,以及管理該節點的網站地圖提供者。
public:
SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title, System::String ^ description);
public SiteMapNode(System.Web.SiteMapProvider provider, string key, string url, string title, string description);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String, description As String)
參數
- provider
- SiteMapProvider
該節點所關聯的 。SiteMapProvider
- key
- String
一個提供者專屬的查詢金鑰。
- url
- String
節點在網站中所代表的頁面網址。
- title
- String
節點的標籤,通常由導航控制顯示。
- description
- String
該節點所代表的頁面描述。
例外狀況
範例
以下程式碼範例示範如何利用 SiteMapNode 建構子 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
備註
XmlSiteMapProvider該類別是 ASP.NET 的預設SiteMapProvider提供者實作,若節點有提供查詢鍵,則使用該SiteMapNode.Url屬性作為查詢鍵(若未提供 URL,則會產生該節點的追蹤識別碼)。 因此,任何 SiteMapNode 提供 URL 且被 使用的 XmlSiteMapProvider 物件,必須在提供者範圍內擁有唯一的 URL。
若未title提供或description,呼叫 或 Description 屬性時Title會回傳欄位String.Empty。
另請參閱
適用於
SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String)
使用指定的網站地圖提供者初始化該類別的新實例 SiteMapNode ,該服務提供者負責管理節點、URL、標題、描述、角色、額外屬性,以及用於本地化的顯式與隱式資源鍵。
public:
SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title, System::String ^ description, System::Collections::IList ^ roles, System::Collections::Specialized::NameValueCollection ^ attributes, System::Collections::Specialized::NameValueCollection ^ explicitResourceKeys, System::String ^ implicitResourceKey);
public SiteMapNode(System.Web.SiteMapProvider provider, string key, string url, string title, string description, System.Collections.IList roles, System.Collections.Specialized.NameValueCollection attributes, System.Collections.Specialized.NameValueCollection explicitResourceKeys, string implicitResourceKey);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string * string * System.Collections.IList * System.Collections.Specialized.NameValueCollection * System.Collections.Specialized.NameValueCollection * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String, description As String, roles As IList, attributes As NameValueCollection, explicitResourceKeys As NameValueCollection, implicitResourceKey As String)
參數
- provider
- SiteMapProvider
該節點所關聯的 。SiteMapProvider
- key
- String
一個提供者專屬的查詢金鑰。
- url
- String
節點在網站中所代表的頁面網址。
- title
- String
節點的標籤,通常由導航控制顯示。
- description
- String
該節點所代表的頁面描述。
- roles
- IList
IList一組被允許瀏覽由 SiteMapNode代表的頁面的角色。
- attributes
- NameValueCollection
NameValueCollection A,這些額外屬性用於初始化。SiteMapNode
- explicitResourceKeys
- NameValueCollection
NameValueCollection A 是用於本地化的明確資源金鑰。
- implicitResourceKey
- String
一個用於本地化的隱式資源金鑰。
例外狀況
備註
XmlSiteMapProvider該類別是 ASP.NET 的預設SiteMapProvider提供者實作,若節點有提供查詢鍵,則使用該SiteMapNode.Url屬性作為查詢鍵(若未提供 URL,則會產生該節點的追蹤識別碼)。 因此,任何 SiteMapNode 提供 URL 且被 使用的 XmlSiteMapProvider 物件,必須在提供者範圍內擁有唯一的 URL。
NameValueCollection建立 的SiteMapNode屬性集合可透過Attributes該屬性取得,並使得無需自訂類別即可輕鬆套用額外屬性於網站地圖節點並擴展其功能。
提供IList一組角色,以提供一組角色,當屬性返回true時,能查看該節點SecurityTrimmingEnabled。 如需詳細資訊,請參閱SecurityTrimmingEnabled。
XmlSiteMapProvider提供者會將該SiteMapNode.Url物業作為查詢金鑰。 因此,任何 SiteMapNode 被 使用的 XmlSiteMapProvider 網域必須在提供者範圍內擁有唯一的 URL。
若未title提供或description,呼叫 或 Description 屬性時Title會回傳欄位String.Empty。
若要程式化指定本地化資源,可以將 的 implicitResourceKey 值設為唯一名稱,用以識別節點的在地資源,或設定 explicitResourceKeys 為 NameValueCollection 一組 name/value 對,其中 name 是用於本地化的節點屬性或自訂屬性,並 value 包含節點屬性或自訂屬性的在地化值。 然後可在適當的 .resx 檔案中設定本地化值。 欲了解更多如何本地化 Title物件、 、 Description及物件的任何自訂屬性 SiteMapNode 的資訊,請參見 How to: Localize Site-Map Data。 關於集合 explicitResourceKeys 的語法需求,請參見 NameValueCollection。