Site-map data is inherently hierarchical, which means that each node can contain zero or more child nodes. The
Note |
---|
Only the |
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
If this code example is placed in a master page, then the
Visual BasicВ | Copy Code |
---|---|
<%@ Page Language="VB" 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"> Public Sub _OnSelectedIndexChanged(ByVal Sender As Object, ByVal e As EventArgs) Response.Redirect(DropDownList1.SelectedItem.Value) End Sub </script> <html xmlns="http://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> |
C#В | Copy Code |
---|---|
<%@ 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"> void _OnSelectedIndexChanged(Object sender, EventArgs e) { Response.Redirect(DropDownList1.SelectedItem.Value); } </script> <html xmlns="http://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 the following:
-
A valid Web.sitemap file that references the ASP.NET file 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:
В Copy Code StartFromCurrentNode="true"
See Also
Tasks
How to: Filter the Nodes Retrieved by SiteMapDataSource Web Server ControlsConcepts
ASP.NET Site Navigation OverviewASP.NET Site Maps
Securing ASP.NET Site Navigation
Securing Data Access