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" "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 >
<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" "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 >
<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.