How to: Programmatically Enumerate Site-Map Nodes
You can use navigation controls to add site navigation to your Web pages with little or no code, but you can also work with site navigation programmatically. When your Web application runs, ASP.NET creates a SiteMap object that reflects the structure of the site map. The SiteMap object, in turn, exposes a collection of SiteMapNode objects that contain properties for each node in the site map.
Navigation controls such as the SiteMapPath control work with the SiteMap and SiteMapNode objects to render the appropriate links automatically.
You can use the SiteMap and SiteMapNode objects in your own code to create custom navigation.
Example
The following code example shows how to display the titles of all of the child nodes for the current page, as long as the current page is listed in the site-map file. If the current page is not listed in the site-map file, the first line of code that uses the SiteMap object will cause a NullReferenceException exception.
<%@ 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">
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim LabelText As String = ""
' Displays the title of the current node.
Label_CurrentNode.Text = SiteMap.CurrentNode.Title
' Determines if the current node has child nodes.
If (SiteMap.CurrentNode.HasChildNodes) Then
For Each ChildNodesEnumerator As SiteMapNode In SiteMap.CurrentNode.ChildNodes
' Displays the title of each node.
LabelText = LabelText & ChildNodesEnumerator.Title & "<br />"
Next
Else
LabelText = LabelText & "No child nodes."
End If
Label_ChildNodes.Text = LabelText
Catch ex As NullReferenceException
Label_CurrentNode.Text = "The current file is not in the site map."
Catch ex As Exception
Label_CurrentNode.Text = "Generic exception: " & e.ToString()
End Try
End Sub ' Page_Load
</script>
<html xmlns="https://www.w3.org/1999/xhtml" >
<head>
<title>Enumerating Child Site Map Nodes</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h2>Current Node</h2>
<asp:Label ID="Label_CurrentNode" Runat="Server"></asp:Label>
<h2>Child Nodes</h2>
<asp:Label ID="Label_ChildNodes" Runat="Server"></asp:Label>
<h2>Verify Against Site Map</h2>
<asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" />
<asp:TreeView ID="TreeView1" Runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
</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">
private void Page_Load(object sender, System.EventArgs e)
{
try
{
string LabelText = "";
// Displays the title of the current node.
Label_CurrentNode.Text = SiteMap.CurrentNode.Title;
// Determines if the current node has child nodes.
if (SiteMap.CurrentNode.HasChildNodes)
{
foreach (SiteMapNode childNodesEnumerator in SiteMap.CurrentNode.ChildNodes)
{
// Displays the title of each node.
LabelText = LabelText + childNodesEnumerator.Title + "<br />";
}
}
Label_ChildNodes.Text = LabelText;
}
catch (System.NullReferenceException ex)
{
Label_CurrentNode.Text = "The current file is not in the site map.";
}
catch (Exception ex)
{
Label_CurrentNode.Text = "Generic exception: " + e.ToString();
}
}
</script>
<html xmlns="https://www.w3.org/1999/xhtml" >
<head>
<title>Enumerating Child Site Map Nodes</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h2>Current Node</h2>
<asp:Label id="Label_CurrentNode" runat="Server"></asp:Label>
<h2>Child Nodes</h2>
<asp:Label id="Label_ChildNodes" runat="Server"></asp:Label>
<h2>Verify Against Site Map</h2>
<asp:SiteMapDataSource id="SiteMapDataSource1" runat="server" />
<asp:TreeView id="TreeView1" runat="server" dataSourceID="SiteMapDataSource1">
</asp:TreeView>
</form>
</body>
</html>
Security
You can hide the links in your navigation structure from users in specified security roles. For more information, see ASP.NET Site-Map Security Trimming.
See Also
Tasks
How to: Programmatically Modify Site-Map Nodes in Memory
Concepts
Securing ASP.NET Site Navigation
Securing Data Access in ASP.NET