SiteMapNodeCollection.Add(SiteMapNode) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将单个 SiteMapNode 对象添加到 SiteMapNodeCollection 集合。
public:
virtual int Add(System::Web::SiteMapNode ^ value);
public virtual int Add (System.Web.SiteMapNode value);
abstract member Add : System.Web.SiteMapNode -> int
override this.Add : System.Web.SiteMapNode -> int
Public Overridable Function Add (value As SiteMapNode) As Integer
参数
- value
- SiteMapNode
要添加到 SiteMapNode 的 SiteMapNodeCollection。
返回
插入 InnerList 的 SiteMapNode 的索引。
例外
value
为 null
。
示例
本部分包含两个代码示例。 第一个代码示例演示如何创建新 SiteMapNodeCollection 集合并向其添加元素。 第二个代码示例演示如何将元素添加到自定义网站地图提供程序。
下面的代码示例演示如何使用 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
下面的代码示例演示自定义网站地图提供程序实现,并演示如何使用 SiteMapNodeCollection 构造函数创建一个新 SiteMapNodeCollection 元素并将元素添加到 SiteMapNodeCollection 该方法 Add 中。
此代码示例是为类提供的大型代码示例的 SiteMapProvider 一部分。
// Implement the GetChildNodes method.
public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
{
SiteMapNodeCollection children = new SiteMapNodeCollection();
// Iterate through the ArrayList and find all nodes that have the specified node as a parent.
lock (this)
{
for (int i = 0; i < childParentRelationship.Count; i++)
{
string nodeUrl = ((DictionaryEntry)childParentRelationship[i]).Key as string;
SiteMapNode parent = GetNode(childParentRelationship, nodeUrl);
if (parent != null && node.Url == parent.Url)
{
// The SiteMapNode with the Url that corresponds to nodeUrl
// is a child of the specified node. Get the SiteMapNode for
// the nodeUrl.
SiteMapNode child = FindSiteMapNode(nodeUrl);
if (child != null)
{
children.Add(child as SiteMapNode);
}
else
{
throw new Exception("ArrayLists not in sync.");
}
}
}
}
return children;
}
protected override SiteMapNode GetRootNodeCore()
{
return RootNode;
}
// Implement the GetParentNode method.
public override SiteMapNode GetParentNode(SiteMapNode node)
{
// Check the childParentRelationship table and find the parent of the current node.
// If there is no parent, the current node is the RootNode.
SiteMapNode parent = null;
lock (this)
{
// Get the Value of the node in childParentRelationship
parent = GetNode(childParentRelationship, node.Url);
}
return parent;
}
' Implement the GetChildNodes method.
Public Overrides Function GetChildNodes(ByVal node As SiteMapNode) As SiteMapNodeCollection
Dim children As New SiteMapNodeCollection()
' Iterate through the ArrayList and find all nodes that have the specified node as a parent.
SyncLock Me
Dim i As Integer
For i = 0 To childParentRelationship.Count - 1
Dim de As DictionaryEntry = CType(childParentRelationship(i), DictionaryEntry)
Dim nodeUrl As String = CType(de.Key, String)
Dim parent As SiteMapNode = GetNode(childParentRelationship, nodeUrl)
If Not (parent Is Nothing) AndAlso node.Url = parent.Url Then
' The SiteMapNode with the Url that corresponds to nodeUrl
' is a child of the specified node. Get the SiteMapNode for
' the nodeUrl.
Dim child As SiteMapNode = FindSiteMapNode(nodeUrl)
If Not (child Is Nothing) Then
children.Add(CType(child, SiteMapNode))
Else
Throw New Exception("ArrayLists not in sync.")
End If
End If
Next i
End SyncLock
Return children
End Function 'GetChildNodes
Protected Overrides Function GetRootNodeCore() As SiteMapNode
Return RootNode
End Function ' GetRootNodeCore()
' Implement the GetParentNode method.
Public Overrides Function GetParentNode(ByVal node As SiteMapNode) As SiteMapNode
' Check the childParentRelationship table and find the parent of the current node.
' If there is no parent, the current node is the RootNode.
Dim parent As SiteMapNode = Nothing
SyncLock Me
' Get the Value of the node in childParentRelationship
parent = GetNode(childParentRelationship, node.Url)
End SyncLock
Return parent
End Function 'GetParentNode
注解
不能将 SiteMapNode 对象添加到只读或固定大小的 SiteMapNodeCollection 集合。 可以通过检查IsReadOnly属性来测试某个属性SiteMapNodeCollection是否为只读。