Share via


HOW TO:以程式設計方式列舉 Site-Map 節點

更新:2007 年 11 月

您可以使用巡覽控制項將站台巡覽加入至網頁 (使用一點程式碼或完全不使用),但您也可以用程式設計方式使用站台巡覽。執行 Web 應用程式時,ASP.NET 會建立 SiteMap 物件,這個物件會反映網站導覽的結構。SiteMap 物件會依次公開 (Expose) SiteMapNode 物件的集合,這些物件含有網站導覽中每個節點的屬性 (Property)。

巡覽控制項 (例如 SiteMapPath 控制項) 會使用 SiteMapSiteMapNode 物件,自動呈現適當的連結。

您可以在自己的程式碼中使用 SiteMapSiteMapNode 物件,以建立自訂巡覽。

範例

下列程式碼範例顯示如何顯示目前網頁所有子節點的標題 (只要目前的網頁有列在網站導覽檔案中)。如果目前的頁面沒有列在網站導覽檔案中,則使用 SiteMap 物件的第一行程式碼會導致 NullReferenceException 例外狀況。

<%@ Page language="VB" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >

  Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Try

      Dim LabelText As String = ""

      ' Displays the title of the current node.
      Label_CurrentNode.Text = SiteMap.CurrentNode.Title

      ' Determines if the current node has child nodes.
      If (SiteMap.CurrentNode.HasChildNodes) Then
        For Each ChildNodesEnumerator As SiteMapNode In SiteMap.CurrentNode.ChildNodes
          ' Displays the title of each node.
          LabelText = LabelText & ChildNodesEnumerator.Title & "<br />"
        Next
      Else
        LabelText = LabelText & "No child nodes."
      End If

      Label_ChildNodes.Text = LabelText

    Catch ex As NullReferenceException
      Label_CurrentNode.Text = "The current file is not in the site map."
    Catch ex As Exception
      Label_CurrentNode.Text = "Generic exception: " & e.ToString()
    End Try

  End Sub ' Page_Load

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>Enumerating Child Site Map Nodes</title>
  </head>
  <body>
    <form id="Form1" method="post" >

      <h2>Current Node</h2>
      <asp:Label ID="Label_CurrentNode" Runat="Server"></asp:Label>

      <h2>Child Nodes</h2>
      <asp:Label ID="Label_ChildNodes" Runat="Server"></asp:Label>

      <h2>Verify Against Site Map</h2>
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
      <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>

    </form>
  </body>
</html>
<%@ Page language="c#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script >

  private void Page_Load(object sender, System.EventArgs e)
  {
    try
    {
      string LabelText = "";

      // Displays the title of the current node.
      Label_CurrentNode.Text = SiteMap.CurrentNode.Title;

      // Determines if the current node has child nodes.
      if (SiteMap.CurrentNode.HasChildNodes)
      {
        foreach (SiteMapNode childNodesEnumerator in SiteMap.CurrentNode.ChildNodes)
        {
          // Displays the title of each node.
          LabelText = LabelText + childNodesEnumerator.Title + "<br />";
        }
      }

      Label_ChildNodes.Text = LabelText;
    }
    catch (System.NullReferenceException ex)
    {
      Label_CurrentNode.Text = "The current file is not in the site map.";
    }
    catch (Exception ex)
    {
      Label_CurrentNode.Text = "Generic exception: " + e.ToString();
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>Enumerating Child Site Map Nodes</title>
  </head>
  <body>
    <form id="Form1" method="post" >

      <h2>Current Node</h2>
      <asp:Label id="Label_CurrentNode" runat="Server"></asp:Label>

      <h2>Child Nodes</h2>
      <asp:Label id="Label_ChildNodes" runat="Server"></asp:Label>

      <h2>Verify Against Site Map</h2>
      <asp:SiteMapDataSource id="SiteMapDataSource1"  />
      <asp:TreeView id="TreeView1"  dataSourceID="SiteMapDataSource1">
      </asp:TreeView>

    </form>
  </body>
</html>

安全性

您可以對具有特定安全性角色的使用者隱藏巡覽結構 (Navigation Structure) 中的連結。如需詳細資訊,請參閱 ASP.NET 網站導覽安全性調整

請參閱

工作

HOW TO:在記憶體中以程式設計方式修改網站導覽節點

概念

設定 ASP.NET 網站巡覽的安全性

設定資料存取的安全性

其他資源

裝載環境中 ASP.NET 應用程式的安全性