Recipe 21.10. Adding Breadcrumbs


You want to use a dynamic menu in your application and display a breadcrumb trail to show the current location within the application and to provide an easy way for the user to navigate back to a previous location.


Implement the dynamic menu solution described in Recipe 21.8 and add a SiteMapPath control to the .master file. Use the .master file as the master page for all pages in your application that require the dynamic menu and the breadcrumb trail.

Example 21-24 shows the .master file used to display the dynamic menu and breadcrumb trail. Figure 21-9 shows the output a sample page with the breadcrumb trail displayed.

Figure 21-9. Sample page with dynamic menu and breadcrumb trail


To improve the user experience, applications commonly display a breadcrumb trail to show the current location within the application and to provide an easy way to navigate back to a previous location. The site navigation infrastructure provided in ASP.NET 2.0 can be used to display a breadcrumb trail using the data in the Web.sitemap file that is also used to as the data source for dynamic menus (see Recipe 21.8).

Once you have the site structure defined in the Web.sitemap file, as described in Recipe 21.8, all that is required is to add a SiteMapPath control to the .master file that will be used as the master page for your application pages that need to display a breadcrumb trail.

The SiteMapPath control uses the site map provider defined by the SiteMapProvider attribute. If the SiteMapProvider attribute is not defined, the SiteMapPath control will use the default SiteMapProvider, which by default is the XmlSiteMapProvider.

The SiteMapPath control displays a series of nodes that match the hierarchy defined in the Web.sitemap file from the root SiteMapNode to the SiteMapNode used to define the currently displayed page. All nodes from the root node to the current page node will be displayed in the breadcrumb trail. By default, all nodes except the current node will be defined as hyperlinks to provide the ability to navigate back up the hierarchy.

If a node in the Web.sitemap file does not have the url attribute defined, the node text will be displayed in the breadcrumb trail but it will not be a hyperlink.

If you want the current node displayed as a hyperlink, you can set the RenderCurrentNodeAsLink attribute to true.

If a page is displayed that does not have a SiteMapNode element in the Web.sitemap file, the breadcrumb trail will not be displayed.

The SiteMapPath control provides many attributes that can be used to define the look and feel of the rendered breadcrumb trail. In addition, it provides the ability to define templates to be used for the root node (RootNodeTemplate), the current node (CurrentNodeTemplate), the nodes in between the root and current node (NodeTemplate), and the separator used between the nodes (PathSeparatorTemplate). The combination of the templates and attributes provides nearly unlimited ways to display your breadcrumb trail.

The XmlSiteMapProvider along with the SiteMapDataSource, Menu, and SiteMapPath controls will meet the menu system needs of most applications. Other controls can be used if they do not meet the needs of your application, such as using the treeView control instead of the Menu control. In addition, all of the controls can be manipulated programmatically if you need to adjust their output more.

See Also

Recipe 21.8; the Menu, SiteMapPath, and treeView controls in the MSDN Library

Example 21-24. Master page with dynamic menu and breadcrumb trail (.master)

 <%@ Master Language="VB" AutoEventWireup="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""> <html xmlns="" > <head  runat="server"> <title>Dynamic Menu Master Page</title> <link rel="Stylesheet" href="css/ASPNetCookbook.css" /> </head> <body> <form  runat="server"> <div > <div > <img src="/books/1/505/1/html/2/images/ASPNETCookbookHeading_blue.gif" /> </div> <div > <asp:SiteMapDataSource  runat="server" /> <asp:menu  runat="server" DataSource Orientation="Horizontal" StaticDisplayLevels="2" BackColor="#f0f0f0" ForeColor="#6B0808" Font-Size="8pt" Font-Bold="true" StaticMenuItemStyle-HorizontalPadding="5" StaticMenuItemStyle-VerticalPadding="0" StaticHoverStyle-BackColor="#6B0808" StaticHoverStyle-ForeColor="#FFFFFF" DynamicMenuItemStyle-BackColor="#f0f0f0" DynamicMenuItemStyle-ForeColor="#0000A0" DynamicMenuItemStyle-BorderStyle="Solid" DynamicMenuItemStyle-BorderColor="#c0c0c0" DynamicMenuItemStyle-HorizontalPadding="5" DynamicMenuItemStyle-BorderWidth="1" DynamicHoverStyle-BackColor="#6B0808" DynamicHoverStyle-ForeColor="#FFFFFF" width="100%" Height="35" >  </asp:menu> </div> </div> <div> <asp:SiteMapPath  runat="server" NodeStyle-Font-Bold="false" NodeStyle-ForeColor="#0000A0" Font-Size="8pt" CurrentNodeStyle-Font-Bold="true" CurrentNodeStyle-ForeColor="#6B0808" CurrentNodeStyle-Font-Underline="false" PathSeparator="&nbsp;&raquo;&nbsp;" > <RootNodeTemplate> &nbsp;&nbsp;<a href="CH21TestBreadcrumbsVB.aspx">Home</a> </RootNodeTemplate> </asp:SiteMapPath> </div> <div> <asp:ContentPlaceHolder  Runat="server" > <div align="center"> <br /> <br /> <h4>Default Content Displayed When No Content Is Provided In Content Pages</h4> </div> </asp:ContentPlaceHolder> </div>   </form> </body> </html> 

ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: