Export (0) Print
Expand All

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.

    NoteNote

    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.

    No code example is currently available or this language may not be supported.
  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"
      ShowStartingNode="~/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.

    NoteNote

    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

Community Additions

ADD
Show:
© 2014 Microsoft