共用方式為


SiteMapProvider.GetParentNode(SiteMapNode) 方法

定義

在衍生類別中覆寫時,擷取特定 SiteMapNode 物件的父節點。

public:
 abstract System::Web::SiteMapNode ^ GetParentNode(System::Web::SiteMapNode ^ node);
public abstract System.Web.SiteMapNode GetParentNode (System.Web.SiteMapNode node);
abstract member GetParentNode : System.Web.SiteMapNode -> System.Web.SiteMapNode
Public MustOverride Function GetParentNode (node As SiteMapNode) As SiteMapNode

參數

node
SiteMapNode

SiteMapNode,要擷取其父節點。

傳回

SiteMapNode

表示 node 之父代的 SiteMapNode,但如果 SiteMapNode 沒有父代,或安全性調整已啟用,而且目前使用者無法存取此父節點,則為 null

注意: GetParentNode(SiteMapNode) 如果父節點屬於不同的提供者,也可能傳回 null 。 在這個案例中,請改用 nodeParentNode 屬性。

範例

下列程式碼範例示範如何在 GetParentNode 實作抽象 SiteMapProvider 類的類別中實作 方法。 會將 SimpleTextSiteMapProvider 物件之間的 SiteMapNode 階層式父子關聯性儲存在介面中 IList ,例如 ArrayList 物件。

此程式碼範例是提供給 類別之較大範例的 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

適用於

另請參閱