ASP.NET 2.0 Unleashed

Using the SiteMapDataSource Control

The SiteMapDataSource control enables you to represent a Site Map declaratively in a page. You can bind navigation controls such as the treeView and Menu controls to a SiteMapDataSource control. You also can bind other controls such as the GridView or DropDownList control to a SiteMapDataSource control.

Imagine, for example, that your website contains the Web.sitemap file in Listing 18.1. Because the default SiteMapProvider is the XmlSiteMapProvider, the SiteMapDataSource control automatically represents the contents of this XML file.

Note

The code samples in this section are located in the SiteMaps application on the CD that accompanies this book.

Listing 18.1. Web.sitemap

<?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="Default.aspx" title="Home" description="The Home Page"> <siteMapNode url="Products/Default.aspx" title="Our Products" description="Products that we offer"> <siteMapNode url="Products/FirstProduct.aspx" title="First Product" description="The description of the First Product" /> <siteMapNode url="Products/SecondProduct.aspx" title="Second Product" description="The description of the Second Product" /> </siteMapNode> <siteMapNode url="Services/Default.aspx" title="Our Services" description="Services that we offer"> <siteMapNode url="Services/FirstService.aspx" title="First Service" description="The description of the First Service" metaDescription="The first service" /> <siteMapNode url="Services/SecondService.aspx" title="Second Service" description="The description of the Second Service" /> </siteMapNode> </siteMapNode> </siteMap>

The Site Map file in Listing 18.1 represents a website with the following folder and page structure:

Default.aspx Products FirstProduct.aspx SecondProduct.aspx Services FirstService.aspx SecondService.aspx

The page in Listing 18.2 illustrates how you can represent a Site Map by binding a treeView control to the SiteMapDataSource control.

Listing 18.2. Default.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>Home</title> </head> <body> <form runat="server"> <div> <asp:SiteMapPath Runat="server" /> <hr /> <asp:TreeView DataSource Runat="server" /> <asp:SiteMapDataSource Runat="server" /> </div> </form> </body> </html>

When you open the page in Listing 18.2, all the elements from the Web.sitemap file are displayed in the treeView control with the help of the SiteMapDataSource control (see Figure 18.1).

Figure 18.1. Displaying a Site Map with a treeView control.

Setting SiteMapDataSource Properties

The SiteMapDataSource control includes several valuable properties that you can set to modify the nodes that the control returns:

  • ShowStartingNode Enables you to hide the starting node.

  • StartFromCurrentNode Enables you to return all nodes starting from the current node.

  • StartingNodeOffset Enables you to specify a positive or negative offset from the current node.

  • StartingNodeUrl Enables you to return all nodes, starting at a node associated with a specified URL.

The most useful of these properties is the ShowStartingNode property. Normally, when you display a list of nodes with a Menu or TreeView control, you do not want to display the starting node (the link to the home page). The page in Listing 18.3 illustrates how you can bind a Menu control to a SiteMapDataSource that has the value False assigned to its ShowStartingNode property.

Listing 18.3. Services/Default.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"> <style type="text/css"> .menuItem { border:solid 1px black; background-color:#eeeeee; padding:4px; margin:1px 0px; } </style> <title>Our Services</title> </head> <body> <form runat="server"> <div> <asp:SiteMapPath Runat="server" /> <hr /> <asp:Menu DataSource StaticMenuItemStyle-Css DynamicMenuItemStyle-Css Runat="server" /> <asp:SiteMapDataSource ShowStartingNode="false" Runat="server" /> </div> </form> </body> </html>

When you open the page in Listing 18.3, only the second-level nodes and descendent nodes are displayed (see Figure 18.2).

Figure 18.2. Hiding the starting node.

The StartFromCurrentNode property is useful when you want to display a list of all nodes below the current node. For example, the page in Listing 18.4 is the Default.aspx page contained in the Products folder. It displays a list of all the product pages contained in the folder.

Listing 18.4. Products/Default.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"> <style type="text/css"> html { font:16px Georgia,Serif; } .productList li { margin:5px; } </style> <title>Our Products</title> </head> <body> <form runat="server"> <div> <h1>Products</h1> <asp:BulletedList DisplayMode="HyperLink" DataTextField="Title" DataValueField="Url" DataSource Css Runat="server" /> <asp:SiteMapDataSource ShowStartingNode="false" StartFromCurrentNode="true" Runat="server" /> </div> </form> </body> </html>

The page in Listing 18.4 contains a BulletedList control bound to a SiteMapDataSource control. Because the SiteMapDataSource control has its StartFromCurrentNode property set to the value TRue and its ShowStartingNode property set to the value False, all immediate child nodes of the current node are displayed (see Figure 18.3).

Figure 18.3. Displaying the contents of a folder.

Категории