ASP.NET 4.0 : Data Source Components (part 9) - The SiteMapDataSource Class - Displaying Site Map Information

12/30/2013 2:40:11 AM

6. The SiteMapDataSource Class

Site maps are a common feature of cutting-edge Web sites. A site map is the graph that represents all the pages and directories found in a Web site. Site map information is used to show users the logical coordinates of the page they are visiting, allow users to access site locations dynamically, and render all the navigation information in a graphical fashion (as shown in Figure 3).

Figure 3. The graphical layout that the MSDN Magazine Web site uses to represent the location of a page in the site hierarchy.

ASP.NET contains a rich navigation infrastructure that allows developers to specify the site structure. I cover site navigation in detail in Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics (Microsoft Press, 2006). For now, it suffices to say that the site map is a hierarchical piece of information that can be used as input for a hierarchical data source control such as SiteMapDataSource. The output of SiteMapDataSource can be bound to hierarchical data-bound controls such as Menu.

6.1 Displaying Site Map Information

The sitemap information can appear in many ways, the simplest of which is to place an XML file named web.sitemap in the root of the application. To give you the essence of site maps and site map data sources, let’s briefly review a few usage scenarios. Suppose you’re writing a Web site and your client asks for a sequence of hyperlinks that indicates the location of the page in the site map. In ASP.NET 1.x, you have to create your own infrastructure to hold site map information and render the page location. (Typically, you would use a configuration file and a user control.) Starting with version 2.0, ASP.NET provides richer support for site maps. You start by creating a configuration file named web.sitemap in the root of the Web application. The file describes the relationship between pages on the site. Your next step depends on the expected output.

If the common representation shown in Figure 3 (a sequence of hyperlinks with a separator) is what you need, add a SiteMapPath control to the page. This control retrieves the site map and produces the necessary HTML markup. In this simple case, there is no need to resort to a site map data source control. If you need to build a more complex hierarchical layout—for example, a tree-based representation—you need the SiteMapDataSource control.

The SiteMapDataSource control pumps site map information to a hierarchical data-bound control (for example, the new TreeView control) so that it can display the site’s structure. Here’s a quick example:

<%@ Page Language="C#" %>
<form runat="server">
<asp:SiteMapDataSource runat="server" ID="MySiteMapSource" />
<asp:TreeView runat="server" DataSourceId="MySiteMapSource" />

Figure 4 shows the final output as it appears to the end user.

Figure 4. The site map information rendered through a TreeView control.

The site map information might look like the following:

<siteMapNode title="Home" url="default.aspx" >
<siteMapNode title="Acknowledgements" url="ack.aspx"/>
<siteMapNode title="References" url="ref.aspx" />
<siteMapNode title="Samples">
<siteMapNode title="Part 1">
<siteMapNode title="Chapter 1" />
<siteMapNode title="Chapter 2" />
<siteMapNode title="Chapter 3">
<siteMapNode title="Dynamic Controls"
url=".../dynctls.aspx" />
<siteMapNode title="ViewState"
url=".../viewstate.aspx" />
<siteMapNode title="Chapter 4" />
<siteMapNode title="Part 2">
<siteMapNode title="Chapter 9">
<siteMapNode title="Site map"
url=".../sitemapinfo.aspx" />
<siteMapNode title="Part 3"
url="samples.aspx?partid=3" />

Note that the url attribute is optional. If the attribute is not defined, the node is intended to be an inert container and won’t be made clickable.


As mentioned, with version 2.0 ASP.NET introduces a new type of data-bound control that was completely unsupported in previous versions—the hierarchical data-bound control. A new base class is defined to provide a minimum set of capabilities—HierarchicalDataBoundControl. The TreeView and Menu controls fall into this category.

  •  ASP.NET 4.0 : Data Source Components (part 8) - The LinqDataSource Class -Lazy Loading and Prefetch, Updating Data
  •  ASP.NET 4.0 : Data Source Components (part 7) - The LinqDataSource Class - Selecting, Sorting, and Filtering Data
  •  ASP.NET 4.0 : Data Source Components (part 6) - The LinqDataSource Class - The Goal of LinqDataSource , Programming Interface of the LinqDataSource Control
  •  ASP.NET 4.0 : Data Source Components (part 5) - The ObjectDataSource Control - Setting Up for Paging , Updating and Deleting Data
  •  ASP.NET 4.0 : Data Source Components (part 4) - The ObjectDataSource Control - Using Parameters, Caching Data and Object Instances
  •  ASP.NET 4.0 : Data Source Components (part 3) - The ObjectDataSource Control - Implementing Data Retrieval
  •  ASP.NET 4.0 : Data Source Components (part 2) - The SqlDataSource Control
  •  ASP.NET 4.0 : Data Source Components (part 1) - Internals of Data Source Controls
  •  ASP.NET 4.0 : Data-Binding Expressions (part 2) - Other Data-Binding Methods
  •  ASP.NET 4.0 : Data-Binding Expressions (part 1) - Simple Data Binding
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone
    Visit movie_stars's profile on Pinterest.