Team LiB
Previous Section Next Section

Working with the SiteMap API

The SiteMapPath control and most likely the menu control (which will be available later on) won't need an explicit data source to show the navigation. Why? The controls use an application programming interface (API) that is available for your direct use as well. The basis of this API is the SiteMap class from the System.Web namespace. The class gives you access to the node (CurrentNode) and root node (RootNode) via four static properties, among other things. Actually, the SiteMap-DataSource control uses these properties to give other data controls access to the navigation data.

In addition to the SiteMap class, it's worth mentioning the SiteMapNode class. This class represents a single element of the navigation. It knows various properties and methods to query the attributes of an entry about which ParentNode, ChildNodes, or the previous, the next, or even all nodes on the same level can be found. Via the IsDescendantOf() method, you can find out whether the element is derived from the second SiteMapNode that was passed over as a parameter.

Accessing the Current Navigation Node

Listing 5-3 shows the use of the SiteMap class. The title of the active navigation entry (SiteMapNode) is retrieved within the Page_Load method of the Master Page, and then it's reused as HTML title of the page.

Listing 5-3: Complete API for Accessing the Site Navigation
Start example
<%@ master language="C#" %>

<script runat="server">

    void Page_Load(object sender, System.EventArgs e)
    {
        if (SiteMap.CurrentNode != null)
        {
            this.LT_HtmlTitle.Text = SiteMap.CurrentNode.Title;
        }
    }

</script>

<html>
<head id="Head1" runat="server">
    <title><ASP:Literal id="LT_HtmlTitle" runat="server"/></title>
</head>
...
End example

Using Custom Attributes

If the page title doesn't have to be identical to the navigation entry (as in the previous example), or if you want to allocate individual data to the single entries, that's fine. For example:

<siteMapNode title="Support"
    description="Supports plans"
    url="navigation5.aspx"
    pagetitle="Here's a custom page title"
/>

You can include as much data as you want by putting it in the app.sitemap file. With the Attributes collection of the SiteMapNode class you can access the data very comfortably at run time. Following this idea, the next example shows the output of a page title, assuming that you've added one to the file:

<%@ master language="C#" %>

<script runat="server">

    void Page_Load(object sender, System.EventArgs e)
    {
        SiteMapNode currentNode = SiteMap.CurrentNode;
        if (currentNode != null)
        {
            if (currentNode.Attributes["pagetitle"] != null)
            {
                this.LT_HtmlTitle.Text = currentNode.Attributes["pagetitle"];
            }
            else
            {
                this.LT_HtmlTitle.Text = currentNode.Title;
            }
        }
    }

</script>

<html>
<head id="Head1" runat="server">
    <title><ASP:Literal id="LT_HtmlTitle" runat="server"/></title>
</head>
...

Enhancing the Provider Model

Like many other new features of ASP.NET, the navigation is based on a provider model. Here, you may use individual sources of data as, for example, a database to build up your navigation. The data generally doesn't have to come out of the app.sitemap file.

If you want to build a new provider, you must first create a class that implements the ISiteMapProvider interface. This interface defines three methods and four properties that are used by the SiteMap class to form the desired object model.

You can find an example of how to implement an individual provider in the documentation for the aforementioned interface.


Team LiB
Previous Section Next Section