Share via


How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server Controls

The SiteMapDataSource control retrieves site-map data from a site-map provider, such as the XmlSiteMapProvider, which is the default site-map provider for ASP.NET. You can configure the SiteMapDataSource control to return the entire collection of site-map nodes or a subset. This is useful when displaying more than one navigation structure on a page, each displaying a separate section of the site map. It is also useful when distributing site-navigation elements across separate master pages in your site, where each master page shows a different portion of the overall site map.

To use these site navigation controls, you must describe the structure of the site in a Web.sitemap file and create the .aspx files listed in your site map.

To create a Web.sitemap file

  1. Create a new file in the root directory of your Web site, and name the file Web.sitemap. Place the following required code in your file:

    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap>
    </siteMap>
    
  2. Create a root siteMapNode element as a child of the siteMap element, defining the following attributes:

    • title   Assign a title to the site-map node, which will be displayed as the link text for the Web page.

    • url   Assign the URL for a Web page. You can use a fully qualified URL or a relative URL such as ~/Default.aspx. The tilde character (~) is used to indicate the root of the application. Later in this procedure, you will create Web pages for each URL listed in the site map because your application will fail if you list a URL that does not exist (or if you duplicate URLs). You can leave this attribute empty, but for the purposes of this example, set it to an .aspx file that you can edit.

    Note

    There can only be one root siteMapNode element in a site map, but that root can contain any number of child siteMapNode elements.

  3. Create a siteMapNode element as a child of the root siteMapNode element. Set the same attributes as in the previous step.

  4. Create a siteMapNode element as a child of the previous siteMapNode element. Set the same attributes as in the previous step. For the purposes of this example, the site map must have three levels of siteMapNode elements.

    The Web.sitemap file will be similar to the following example:

    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap>
      <siteMapNode title="Home" url="~/Default.aspx" roles="*">
        <siteMapNode title="Services" url="~/Services.aspx " >
          <siteMapNode title="Training" url="~/Training.aspx" />
        </siteMapNode>
        <siteMapNode title="Products" url="" />
      </siteMapNode>
    </siteMap>
    
  5. Save the Web.sitemap file and close it.

To add site navigation to a Web page

  1. Create an .aspx page for each of the files you listed in the url attributes of your Web.sitemap file. In each of the .aspx files, replace the code with the following to display the site map in various controls that display site-map data. If an .aspx file is not listed in the site map, it cannot display a site-navigation control.

    <%@ Page Language="VB" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
      <title>Simple Navigation Controls</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
    
      <h2>Using SiteMapPath</h2>
      <asp:SiteMapPath ID="SiteMapPath1" Runat="server">
      </asp:SiteMapPath>
    
    
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
      <h2>Using TreeView</h2>
      <asp:TreeView ID="TreeView1" Runat="Server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    
      <h2>Using Menu</h2>
      <asp:Menu ID="Menu2" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:Menu>
    
      <h2>Using a Horizontal Menu</h2>
      <asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1"
        Orientation="Horizontal" 
        StaticDisplayLevels="2" >
      </asp:Menu>
    
      </div>
      </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
      <title>Simple Navigation Controls</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
    
      <h2>Using SiteMapPath</h2>
      <asp:SiteMapPath ID="SiteMapPath1" Runat="server">
      </asp:SiteMapPath>
    
    
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
      <h2>Using TreeView</h2>
      <asp:TreeView ID="TreeView1" Runat="Server" DataSourceID="SiteMapDataSource1">
      </asp:TreeView>
    
      <h2>Using Menu</h2>
      <asp:Menu ID="Menu2" Runat="server" DataSourceID="SiteMapDataSource1">
      </asp:Menu>
    
      <h2>Using a Horizontal Menu</h2>
      <asp:Menu ID="Menu1" Runat="server" DataSourceID="SiteMapDataSource1"
        Orientation="Horizontal" 
        StaticDisplayLevels="2" >
      </asp:Menu>
    
      </div>
      </form>
    </body>
    </html>
    
  2. Save your files.

To change the starting node returned to the navigation control

  1. In the .aspx page that is three levels deep in your site map, locate the SiteMapDataSource control, which might look like following line of code:

    <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
  2. Change the above line of code to look like the following:

    <asp:SiteMapDataSource 
      ID="SiteMapDataSource1" 
      Runat="server"
      StartingNodeUrl="~/Services.aspx" />
    
  3. Save your file and view it in a browser.

    The navigation structure in the Training.aspx page is different from the other two pages. The structures begin at the second node. The SiteMapPath control is unaffected because it obtains site-map data directly from the provider and does not need a SiteMapDataSource control.

    The following other options exist for changing the starting node:

    • Setting the StartFromCurrentNode property to true retrieves the site-map structure, beginning with the node for the current page.

Setting the StartingNodeOffset property to 2 retrieves the site-map structure, beginning two nodes deep from the root node and following the path to the current page.

To hide the starting node

  1. In the .aspx page that is three levels deep in your site map, locate the SiteMapDataSource control, which might look like following line of code:

    <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
    
  2. Change the above line of code to look like the following:

    <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server"
      StartingNodeUrl="~/Services.aspx" />
    
  3. Save your file and view it in a browser.

    The navigation structure in this page is different from the other two pages. The result is different from setting the StartingNodeOffset property to 1 or setting the StartingNodeUrl property to ~/Services.aspx because the site-map node collection is not restricted to one branch of the entire site map, which allows you to see the Products node as well.

    Note

    You can also set the StartingNodeOffset property to a negative number if the starting node is nested deeper than the root node of the site map. This is often useful when the StartFromCurrentNode property is set to true and you want to display the site map starting from the parent of the current node.

See Also

Tasks

How to: Customize the Appearance of SiteMapPath Web Server Controls

Concepts

Implementing ASP.NET Site-Map Providers

ASP.NET Site-Map Security Trimming

Securing ASP.NET Site Navigation

Securing Data Access

Other Resources

ASP.NET Application Security in Hosted Environments