A site map is a graph representing all the pages and directories in a web site. Site map information shows the logical coordinates of the page and allows dynamic access of the pages and render all navigational information in a graphical way. ASP.Net contains a rich navigation infrastructure which allows the developers to specify the site structure. Since the site map information is hierarchical in nature, it can be used as input for a hierarchical data source control such as SiteMapDataSource. The output of a SiteMapDataSource can be bound to hierarchical data-bound controls such as Menu or TreeView

Introduction: 

A site map is a graph representing all the pages and directories in a web site. Site map information shows the logical coordinates of the page and allows dynamic access of the pages and render all navigational information in a graphical way. ASP.Net contains a rich navigation infrastructure which allows the developers to specify the site structure. Since the site map information is hierarchical in nature, it can be used as input for a hierarchical data source control such as SiteMapDataSource. The output of a SiteMapDataSource can be bound to hierarchical data-bound controls such as Menu or TreeView.

Site Map Basics: 

ASP.Net navigation is flexible, configurable, and pluggable. It consists of three components:

You can customize or extend each of these separately. For example for appearance purpose, you can bind different controls to the SiteMapDataSource. On the other hand you can create a custom site map provider to receive information from a database.

 

Simple Site Map Design: 

Let us start with a quick example where the SiteMapDataSource control passes the site map information to a hierarchical data-bound control so that it can display the site’s structure.

 

The following Code shows a simple imaginary site map for an author’s site:

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<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="mySiteMapDataSource" runat="server" />

   

    </div>

        <asp:TreeView ID="mytreeview" runat="server" DataSourceID="mySiteMapDataSource" Height="307px"

            Width="173px">

        </asp:TreeView>

    </form>

</body>

</html>

 

 The SiteMapDataSource is filled by the following Site map:

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="" title="Home"  description="Hanif-Online Homepage">

                  <siteMapNode url="" title="Publication"  description="Books by the Author">

            <siteMapNode url="" title=" Books"  description="" />

            <siteMapNode url="" title="Projects"  description="" />

      </siteMapNode>

      <siteMapNode url="" title="Examination Support"  description="Solved Papers">

            <siteMapNode url="" title=" University Examinations"  description="" />

            <siteMapNode url="" title="Professional Courses"  description="" />

      </siteMapNode>

      </siteMapNode>   

</siteMap>

The final output appears as:
 


 The Programming Interface of SiteMapDataSource:

In out simple example, we have not used the SiteMapDataSource object much. But the  SiteMapDataSource class have the following properties which allows the developers to change the root node and change display and provider.

Properties of SiteMapDataSource:
 

Property

Description

Provider

Indicates the site map provider object associated with the data source control

ShowStartingNode

Indicates whether the starting node is retrieved and displayed. It is True by default.

SiteMapProvider

It gets and sets the name of the site map provider associated with the instance of the control.

StartFromCurrentNode

Indicates whether the node tree is retrieved relative to the current page. A Boolean value, default is false.

StartingNodeOffset

Gets and sets a positive or negative offset from the starting node that determines the root hierarchy exposed by the control. This property is set to 0 by default.

StartingNodeUrl

Indicates the URL in the site map in which the node tree is rooted.

By default, the starting node is the root node of the hierarchy, but you can change the starting node through a pair of mutually exclusive properties – StartFromCurrentNode and the StartingNodeUrl. If you want to explicitly set the URL of the root page of the hierarchy, then keep the StartFromCurrentNode property to false and if it is set to be true, then ensure that the StartingNodeUrl evaluates to an empty string.

The StartingNodeOffset property allows restricting the nodes of the site map that are actually displayed. The default value 0 indicates that the root hierarchy exposed by the SiteMapDataSource control is the same as the starting node. A value more than 0 puts the node that number of levels down the hierarchy, starting from the root to the requested node, and uses the node found as the root. A negative offset, on the other hand, ensures that the specified number of child levels will be displayed if possible.

Creating a Custom Site Map Provider: 

At times you need to create your own site map provider. The reasons behind such decision could be either of the following:

 
If the site map of our previous example, were to be stored in a database, we would have stored the node specifications in the database along with a field for ParentNodeID, which would have helped to maintain hierarchical structure of the site map, provided by the XML document otherwise.
 

ID

Url

Title

Description

ParentNodeID

1

#

Home

Home

<NULL>

2

#

Publication

All Publications

1

3

#

Examination Support

Solved Papers

1

4

#

Books

All Published Books

2

5

#

Projects

Projects Undertaken

2

6

#

University Examinations

University Papers

3

7

#

Professional Examinations

Professional Exam Papers

3

A simple stored procedure will retrieve the site map data stored in a SQL Server table. Let us assume, we will use the Northwind database and store the data in a table called SiteMap.

The procedure:

CREATE PROCEDURE GetSiteMap AS

SELECT * FROM SiteMap

GO

Next, we should create the site map provider. We should derive this provider from the StaticSiteMapProvider class.

public class SqlSiteMapProvider : StaticSiteMapProvider

{ ... }

The provider needs the following three information, which we should store in the <sitemap> section of the web.config file.

Next you can configure your web application to use the custom provider, i.e.,  SqlSiteMapProvider and supply the required three pieces of information using the <sitemap> section of the web.config file.

<system.web>

          <siteMap defaultProvider=”SqlSiteMapProvider”>

               <providers>

                   <add name=”SqlSiteMapProvider” type=” SqlSiteMapProvider”

                        providerName=”System.Data.SqlClient”

                        connectionString=”Data Source=localhost;Initial Catalog=Northwind;

                           Integrated Security=SSPI” storedProcedure=”GetSiteMap” />

                </providers>

            </sitemap>

 

              ...

  </system.web>

Conclusion: 

We have discussed how to create a site map for your site. ASP.Net provides a wide range of navigational features. We have looked into the site map model, which allows you to define the navigation structure of your site and bind it directly to rich controls. There are also the Multiview and Wizard controls which let you put a series of steps in a single page and rich navigational controls like TreeView and Menu. Use of these controls is not limited to navigation purpose. We will see later, these controls can be used to display XML data.      

[Download Project]