在 Web 页面中使用导航控件添加页面导航功能只需要编写少量代码(甚至不需要代码)即可实现,但是也可以对网站导航进行可编程控制。当 Web 应用程序运行时,ASP.NET 会创建反映网站地图结构的 SiteMap 对象。该 SiteMap 对象在运行时会暴露包含每个网站地图节点属性的 SiteMapNode 对象集。

类似 SiteMapPath 控件的导航控件与 SiteMapSiteMapNode 对象协同合作,并自动呈现出合适的链接内容。

可以在代码中使用 SiteMapSiteMapNode 对象自定义网站导航。

实例

下例代码说明了如何显示当前页面的所有子节点标题。网站地图文件中必须列有当前页面的路径,如果网站地图文件没有列出当前页面的路径,代码第一行对 SiteMap 对象的使用将引发 NullReferenceException 异常。

<%@ 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 runat="server">

  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>
  <head>
    <title>Enumerating Child Site Map Nodes</title>
  </head>
  <body>
    <form 
      method="post">

      <h2>Current Node</h2>
      <asp:Label ></asp:Label>

      <h2>Child Nodes</h2>
      <asp:Label ></asp:Label>

      <H2>Verify Against Site Map</H2>
      <asp:SiteMapDataSource  />
      <asp:TreeView 
        dataSourceID="SiteMapDataSource1">
      </asp:TreeView>
      
    </form>
  </body>
</html>

安全性

可以针对于特定安全角色的用户在导航结构隐藏相应的链接信息。

相关文章:

  • 2021-08-07
  • 2022-12-23
  • 2021-12-28
  • 2022-02-14
  • 2021-07-17
  • 2021-10-09
  • 2021-11-14
  • 2022-03-08
猜你喜欢
  • 2021-07-03
  • 2021-08-02
  • 2021-07-09
  • 2022-02-26
  • 2022-12-23
  • 2022-03-05
  • 2021-11-08
相关资源
相似解决方案