ASP.NET Lab

The Best Web, The Best Future

博客园 首页 新随笔 订阅 管理

在 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 id="Form1" runat="server"
      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>

安全性

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

posted on 2006-12-11 17:37  Laeb  阅读(352)  评论(0编辑  收藏  举报