May 22, 2019
Hot Topics:

ASP.NET 2.0's Sophisticated Site Navigation Features

  • September 30, 2004
  • By Thiru Thangarathinam
  • Send Email »
  • More Articles »

Now that you have modified the master page, create a new content page named Members.aspx file and modify its code to look like the following:

<%@ Page Language="C#" MasterPageFile="~/Navigation.master"
         Title="Members Page"%>
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1"
             ID="Content1" Runat="Server">
   This is the members page

Now, navigate to the default.aspx file using the browser. In the default page, click on the Members hyperlink using the menu or the treeview control. Figure 3 displays the resulting output.

Click here for a larger image.

Figure 3. The Breadcrumb Control in Action

The screenshot in Figure 3 shows the breadcrumb control in action. Because you have navigated to the members page, the breadcrumb control displays the link to the default page as well as the description of the current page.

Obtain Site Navigation Data Programmatically

You also can retrieve site navigation data programmatically. One of the important classes that you need to use when doing this is the SiteMap class. This class provides a number of static methods, the most important one being the CurrentNode property. For example, you can invoke the SiteMap.CurrentNode to reference a piece of navigation data matching the currently executing page. The CurrentNode property returns an instance of a SiteMapNode object that corresponds to the current page. Similar to the previous examples, the site navigation feature determines the correct node to return based upon navigation data that is stored in the web.sitemap file.

Once you have an instance of the SiteMapNode object instance, you can perform the following tasks:

  • Retrieve properties of a node, including Url, Title, and Description
  • Obtain a node's parent as well as a node's children
  • Navigate through the sibling nodes before and after the current node
  • Obtain a reference to the SiteMapProvider instance that returned this node

Once you have a reference to a SiteMapProvider, you can search the site navigation data for a specific node based on URL. This allows you to obtain references to SiteMapNode instances anywhere in your site-navigation data. The combination of finding arbitrary SiteMapNode instances and the ability to navigate through site navigation data from any SiteMapNode allows you to easily walk through your site's navigation data.

Apart from the web.sitemap file, you also can store the navigational data in other data stores, such as some other XML file or relational data in the database. In that case, you then can author a custom provider that derives from SiteMapProvider. SiteMapProvider instances are responsible for returning SiteMapNode instances that can then be consumed by pages and server controls.

The following example enhances the Members.aspx page by adding some code in the Page_Load event. Here is the modified code of the Members.aspx page:

<%@ Page Language="C#" MasterPageFile="~/Navigation.master"
         Title="Members Page"%>
<script runat="Server">
   void Page_Load(object sender, EventArgs e)
      Response.Write("The currently selected root node is: " +
                      SiteMap.CurrentNode.Description + "<br>");
      Response.Write("The Parent for the currently selected node
                      is : "  +
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1"
             ID="Content1" Runat="Server">
   This is the members page

You use the code in Page_Load to retrieve the details of the currently selected node. Once you've obtained the reference to the current node, you then can retrieve the description as well. The code also demonstrates how to get the reference to the parent node from the current node by invoking the current node's ParentNode property. If you run the above code, you will get an output that is somewhat similar to Figure 4.

Click here for a larger image.

Figure 4. The Details of the Currently Selected Node

In the above screenshot, the top of the page displays the description of the currently selected node, as well as the description of the parent node.

Sophisticated Site Navigation for Your Web Applications

ASP.NET 2.0 site navigation provides a number of new productivity enhancements that should excite developers. The features you saw in this article provide a good representation of ASP.NET 2.0's site-navigation features. Once you get familiar with them, you will have a rich set of tools and techniques for creating sophisticated site navigational capabilities in your Web applications.

Download the Code

Download the accompanying source code here.

About the Author

Thiru Thangarathinam has six years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. He also possesses a thorough understanding of the software life cycle (design, development, and testing). He holds several certifications, including MCAD for .NET, MCSD, and MCP. Thiru is an expert with ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET Remoting. Thiru also has authored numerous books and articles. Contact him at thiruthangarathinam@yahoo.com.

Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date