Partager via


How to: Display Site-Map Data in Non-Hierarchical Web Server Controls

Site-map data is inherently hierarchical, which means that each node can contain zero or more child nodes. The TreeView and Menu controls are designed to work with hierarchical data. However, you can bind site-map data to non-hierarchical controls, such as the DropDownList, CheckBoxList, and other controls that display data in a linear, or flat, format.

Note

Only the SiteMapPath control and controls that support the INavigateUIData interface render site-map nodes as links.

Example

The following code example uses a DropDownList control to display the site-map data from a Web.sitemap file.

When a client selects an item in the drop-down list, the browser is redirected to the selected page. This is accomplished by calling the Redirect method in the OnSelectedIndexChanged event handler.

If this code example is placed in a master page, then the StartFromCurrentNode property in the SiteMapDataSource control will ensure that the drop-down list always displays a site map that begins at the currently executing page.

<%@ Page Language="VB"  AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  Public Sub _OnSelectedIndexChanged(ByVal Sender As Object, ByVal e As EventArgs)
    Response.Redirect(DropDownList1.SelectedItem.Value)
  End Sub
</script>

<html xmlns="https://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="Server"
          StartFromCurrentNode="true"
          ShowStartingNode="false" />
      <asp:DropDownList ID="DropDownList1" Runat="Server" 
          DataSourceID="SiteMapDataSource1"
          AutoPostBack="True" 
          DataTextField="Title" 
          DataValueField="Url"
          OnSelectedIndexChanged="_OnSelectedIndexChanged" >
      </asp:DropDownList>
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  void _OnSelectedIndexChanged(Object sender, EventArgs e)
  { 
    Response.Redirect(DropDownList1.SelectedItem.Value);
  }
</script>

<html xmlns="https://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>DropDownList Bound to SiteMapDataSource</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="Server"
          StartFromCurrentNode="true"
          ShowStartingNode="false" />
      <asp:DropDownList ID="DropDownList1" Runat="Server" 
          DataSourceID="SiteMapDataSource1"
          AutoPostBack="True" 
          DataTextField="Title" 
          DataValueField="Url"
          OnSelectedIndexChanged="_OnSelectedIndexChanged" >
      </asp:DropDownList>
    </div>
    </form>
</body>
</html>

If the page does not contain any child nodes, then the drop-down list will be empty. If the client selects an item for which there is no URL property set in the Web.sitemap file, then the client is redirected to the home page of the application.

Compiling the Code

  • This example requires a valid Web.sitemap file that references the ASP.NET Web page that contains the code example. If you put this example code in a file that is not listed in one of the nodes in the Web.sitemap file, remove the following property from the control:

    StartFromCurrentNode="true"
    

See Also

Tasks

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

Concepts

ASP.NET Site Maps

Securing ASP.NET Site Navigation

Securing Data Access in ASP.NET

Other Resources

ASP.NET Site Navigation

ASP.NET Application Security in Hosted Environments