Tuesday, March 25, 2008

Bread Crumbs in ASP.NET 2.0


Bread Crumbs are not considered a good appetizer but it is a valuable feature introduced in ASP.NET 2.0. Bread Crumbs allows you to view your current location in the website. This means if you are in the horror movies section of the website then it will indicate that you are indeed in the horror movies section by presenting the hierarchical view of your position. By using Bread Crumbs you can navigate back and forth in the categories. In this article I will demonstrate that how you can use a Bread Crumb in your web application.

Understanding the Scenario:

Let's first understand the scenario before talking about the implementation of the application. Suppose you are building a website that will contain information about movies and books. Each of the category can have sub categories. Movies can be comedy or horror and books can be fiction or horror. In your application you might have different folders for each of the category. Take a look at the screen shot below which shows the folders layout for the application.

As, you can see that the books folder (Books) contains the fiction and horror folders. Similarly the Movies folder contains the comedy and horror folder and each of the category has its own web form. This hierarchy is reflected in the Web.sitemap file.

Web.SiteMap File:

Take a look at the Web.sitemap file which shows the hierarchy of the files and folders.

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

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

<siteMapNode url="~/Default.aspx" title="Home" description="">

<siteMapNode url="~/Books/Default.aspx" title="Books" description="">

<siteMapNode url="~/Books/Fiction/FictionBooks.aspx" title="Fiction" />

<siteMapNode url="~/Books/Horror/HorrorBooks.aspx" title="Horror" />


<siteMapNode url="~/Movies/Default.aspx" title="Movies" >

<siteMapNode url="~/Movies/Comedy/ComedyMovies.aspx" title="Comedy Movies" />

<siteMapNode url="~/Movies/Horror/HorrorMovies.aspx" title="Horror Movies" />




Once, you have the sitemap file ready you need to create a navigation system for the user. ASP.NET 2.0 provides three new navigation controls namely TreeView, Menu and SiteMapPath. The SiteMapPath navigation works in a different way since you need to be on the page in order to see the previous pages. I will use the TreeView in this article and populate it using the Web.sitemap file.

Populating the TreeView Control with Web.SiteMap:

Since, the navigation will be displayed on most of the pages of the application it is a good idea to place it inside the Master Page. Take a look at the code below which shows how to populate the TreeView control with the data from the Web.sitemap.

<asp:TreeView ID="tvMenu" DataSourceID="SiteMapDataSource1" runat="server" BackColor="PeachPuff" BorderColor="#C0C000" ImageSet="Arrows">

<ParentNodeStyle Font-Bold="False" />

<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />

<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"

VerticalPadding="0px" />

<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px"

NodeSpacing="0px" VerticalPadding="0px" />


<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

Displaying the Bread Crumbs Using SiteMapPath Control:

Finally, we need to display the Bread Crumbs on the page. Displaying Bread Crumbs is pretty simple if you use the SiteMapPath control. Simply, place a SiteMapPath control on the master page and set its properties as follows:

<asp:SiteMapPath ID="SiteMapPath1" runat="server">


<asp:HyperLink ID="hlRoot" runat="server" Text='<%# Eval("title") %>' NavigateUrl='<%# Eval("url") %>' />



<asp:HyperLink ID="hlCurrentNodeTemplate" runat="server" Text='<%# Eval("title") %>' NavigateUrl='<%# Eval("url") %>' />



There are different types of templates available for the SiteMapPath control. I have demonstrated the RootNodeTemplate and the CurrentNodeTemplate. The RootNodeTemplate allows you to define the root of the Bread Crumb and the CurrentNodeTemplate allows you to define the currently active node of the Bread Crumb.

Take a look at the effect of the Bread Crumbs in the screen shots below:

No comments: