Using the SiteMap Class


Using the SiteMap Class

Under the covers, the SiteMapDataSource control represents the contents of the SiteMap class. The SiteMap class represents an application's Site Map regardless of whether the Site Map is stored in an XML file, a database, or some other data source. The class is a memory-resident representation of Site Map data.

All the properties exposed by the SiteMap class are shared (static) properties:

  • CurrentNode Enables you to retrieve the SiteMapNode that corresponds to the current page.

  • Enabled Enables you to determine whether the Site Map is enabled.

  • Provider Enables you to retrieve the default SiteMapProvider.

  • Providers Enables you to retrieve all the configured SiteMapProvders.

  • RootNode Enables you to retrieve the root SiteMapNode.

The CurrentNode and RootNode properties return a SiteMapNode object. Because a Site Map can contain only one root node, and the root node contains all the other nodes as children, the RootNode property enables you to iterate through all the nodes in a Site Map.

The Provider property returns the default SiteMapProvider. You can use this property to access all the properties and methods of the SiteMapProvider class, such as the FindSiteMapNode() and GetParentNode() methods.

The SiteMap class also supports a single event:

  • SiteMapResolve Raised when the current node is accessed.

You can handle this event to modify the node returned when the current node is retrieved. For example, the Global.asax file in Listing 18.5 automatically adds a new node when the current page does not include a node in the Site Map.

Listing 18.5. Global.asax

[View full width]

<%@ Application Language="VB" %> <%@ Import Namespace="System.IO" %> <script runat="server">     Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)         AddHandler SiteMap.SiteMapResolve, AddressOf SiteMap_SiteMapResolve     End Sub     Private Function SiteMap_SiteMapResolve(ByVal sender As Object, ByVal e As  SiteMapResolveEventArgs) As SiteMapNode         If SiteMap.CurrentNode Is Nothing Then             Dim url As String = e.Context.Request.Path             Dim title As String = Path.GetFileNameWithoutExtension(url)             Dim NewNode As New SiteMapNode(e.Provider, url, url, title)             NewNode.ParentNode = SiteMap.RootNode             Return NewNode         End If         Return SiteMap.CurrentNode     End Function </script> 

The Application_Start() event handler in Listing 18.5 executes only once when the application first starts. The handler adds a SiteMapResolve event handler to the SiteMap class.

Whenever any control retrieves the current node, the SiteMap_SiteMapResolve() method executes. If there is no node that corresponds to a page, then the method creates a new node and returns it.

The About.aspx page in Listing 18.6 is not included in the Web.sitemap file. However, this page includes a SiteMapPath control. The SiteMapPath control works correctly because the About.aspx page is dynamically added to the Site Map when you access the page (see Figure 18.4).

Figure 18.4. Adding nodes to a Site Map dynamically.


Listing 18.6. About.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/ xhtml11/DTD /xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>About</title> </head> <body>     <form  runat="server">     <div>     <asp:SiteMapPath                  Runat="server" />     <hr />     <h1>About Our Company</h1>     </div>     </form> </body> </html> 

Using the SiteMapNode Class

All pages and folders in a Site Map are represented by instances of the SiteMapNode class. The SiteMapNode class contains the following properties:

  • ChildNodes Returns the child nodes of the current node.

  • Description Returns the description of the current node.

  • HasChildNodes Returns true when the current node has child nodes.

  • Item Returns a custom attribute (or resource string).

  • Key Returns a unique identifier for the current node.

  • NextSibling Returns the next sibling of the current node.

  • ParentNode Returns the parent node of the current node.

  • PreviousSibling Returns the previous sibling of the current node.

  • Provider Returns the SiteMapProvider associated with the current node.

  • ReadOnly Returns true when a node is read-only.

  • ResourceKey Returns the resource key associated with the current node (enables localization).

  • Roles Returns the user roles associated with the current node.

  • RootNode Returns the Site Map root node.

  • Title Returns the title associated with the current node.

  • Url Returns the URL associated with the current node.

The SiteMapNode class also supports the following methods:

  • Clone() Returns a clone of the current node.

  • GetAllNodes() Returns all descendent nodes of the current node.

  • GetdataSourceView() Returns a SiteMapDataSourceView object.

  • GetHierarchicalDataSourceView() Returns a SiteMapHierarchicalDataSourceView.

  • IsAccessibleToUser() Returns true when the current user has permissions to view the current node.

  • IsDescendantOf() Returns true when the current node is a descendant of a particular node.

By taking advantage of the SiteMap and SiteMapNode classes, you can work directly with Site Maps in a page. For example, imagine that you want to display the value of the SiteMapNode title attribute in both the browser's title bar and in the body of the page. Listing 18.7 demonstrates how you can retrieve the value of the Title property associated with the current page programmatically.

Listing 18.7. Products/FirstProduct.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/ xhtml11/DTD /xhtml11.dtd"> <script runat="server">     Private Sub Page_Load()         If Not Page.IsPostBack Then             Dim currentNode As SiteMapNode = SiteMap.CurrentNode             Me.Title = currentNode.Title             ltlBodyTitle.Text = currentNode.Title             lblDescription.Text = currentNode.Description         End If     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>First Product</title> </head> <body>     <form  runat="server">     <div>     <h1><asp:Literal  runat="server" /></h1>     <asp:Label                  Runat="server" />     </div>     </form> </body> </html> 

When you open the page in Listing 18.7, the Page_Load() event handler grabs the current SiteMapNode and modifies the Page Title property. The handler also assigns the value of the Title property to a Literal control contained in the body of the page. Finally, the value of the SiteMapNode's Description property is assigned to a Label control (see Figure 18.5).

Figure 18.5. Retrieving Site Map node properties.


Note

It would make sense to place the code in Listing 18.7 in a Master Page. To learn more about Master Pages, see Chapter 5, "Designing Web Sites with Master Pages."





ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net