ASP.NET Breadcrumbs with C#
By utilizing breadcrumb navigation you allow your viewers to easily trace their path taken to the current location and back track if necessary.
A breadcrumb navigation usually looks something like:
Main Page > Page Design : ASP.NET Breadcrumbs with C#
Within this article you will learn how to create breadcrumb navigation using ASP.NET and C#.
In the above example we see that the viewer started on the ’Main Page’, then moved to the ‘Page Design’ section, then selected the ‘ASP.NET Breadcrumbs with C#’ page, where they currently reside. The Main Page and Page Design will be hyperlink so that you can go directly to the above sections.
The first part, ‘Main Page’ is where the user is starting; typically something like ‘Home’ or ‘Home Page’ will be used. However, we will use a variable in our code so that the user may decide what text they would like to use. The next part ‘Page Design’ is a sub-category that holds the page that the user is currently viewing, ‘ASP.NET Breadcrumbs with C#’. We know that this is the final resting spot because the ‘:’ preceding the page name indicates so. Finally, the ‘>’ is used as a separator for categories, this also is a variable, allowing for a choice of symbol.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;
// We include the System.Text namespace so we can use the StringBuilder object later.
namespace BreadCrumbs
{
///
/// Summary description for ctrlBreadCrumbs.
///
public class ctrlBreadCrumbs : System.Web.UI.WebControls.WebControl
{
///
/// The 3 variables below, Separator, RootName, directoryNameSpacer can be changed to meet your needs
/// PageTitle is pulled from the CodeBehind of the page
///
public string Separator = ” > “;
public string RootName = “Home Page”;
public char directoryNameSpacer = ‘_’;
private string _PageTitle;
// Above are our variables that we can set:
// Separator is the ‘>’ symbol
// RootName is the homepage anchor text
// directoryNameSpacer is the naming scheme for my directories, for example: search_engine_optimization or website_design (notice the URL in the address bar above)
public string PageTitle
{
get
{
return _PageTitle;
}
set
{
_PageTitle = value;
}
}
///
/// Render this control to the output parameter specified.
///
///
The HTML writer to write out to
// get the url root, like www.domain.com
string strDomain = Page.Request.ServerVariables["HTTP_HOST"].ToString();
strDomain.Trim(); // Trim removes leading and trailing whitespace
sbResult.Append ( “” + RootName + “” + Separator );
// gets dir(s), like subdirectory/subsubdirectory/file.aspx
string scriptName = Page.Request.ServerVariables["SCRIPT_NAME"].ToString();
// find the last ‘/’ and Remove the text after it as it’s the file name
int lastSlash = scriptName.LastIndexOf(’/'); // returns the # of chars. from right to /
string pathOnly = scriptName.Remove(lastSlash, (scriptName.Length - lastSlash));
// create breadcrumb HTML for the directory name(s)
// We Remove the first “/” otherwise when you split the string the first item in array is empty
pathOnly = pathOnly.Substring(1);
string[] strDirs = pathOnly.Split(’/');
int nNumDirs = strDirs.Length;
// URLs for breadcrumbs
string strURL = “”;
for (int i=0; i
{
strURL += “/”+strDirs[i];
// convert underscores to spaces
strDirs[i] = strDirs[i].Replace(directoryNameSpacer,’ ‘);
int counter = i+1;
if (counter != nNumDirs)
{
sbResult.Append ( “” + strDirs[i] + “” + Separator );
}
else
{
// This is the last directory so don’t tack on Separator
sbResult.Append ( “” + strDirs[i] + “” ); }
}
// write the PageTitle, pulled from the CodeBehind!
sbResult.Append ( ” : ” + this.PageTitle );
output.Write ( sbResult.ToString() );
}
}
}
We are going to use an ASP.NET User control (.ascx) so that we may simply add this control to our pages making it easy to maintain and modify later if we should choose to. For example, if we have 100 pages and we want to modify the separator in our breadcrumb navigation from the “>†symbol to an image we will only have to modify 1 page, our .ascx page and not all 100 .aspx pages.
<%@ Register TagPrefix=”bc” Namespace=”BreadCrumbs” Assembly=”BreadCrumbs” %>
protected BreadCrumbs.ctrlBreadCrumbs bc1;
private string _strHeaderText;
public string HeaderText
{
get
{
return _strHeaderText;
}
set
{
_strHeaderText = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
bc1.PageTitle = HeaderText;
}
Creating Our Web Pages (.aspx). For each page (.aspx) you create for your website use a literal . This allows us to define the actual title for our page in the code behind. By defining the title of the page in the code behind we gain the benefit of only having to assign it once and have it not only be used for the title of our page, but also be used in our breadcrumb navigation.
Your code behind would look like this:
public class CodeBehind_for_page: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal lblPageTitle;
protected NameSpace.headerBreadCrumb header;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
string PageTitle = “ASP.NET Breadcrumbs with C#”;
lblPageTitle.Text = PageTitle;
header.HeaderText = PageTitle;
}
}
We created a class for our breadcrumb navigation and used that class for an ASP.NET User control (.aspx). This allows us to simply drop the User control into each web page (.aspx) we create. Once we have our User control in our page we assign the PageTitle in the code behind and this gets used for the site.
Copyright @ Abhishek Arya 2007

- Login Control in Visual Studio 2005
- Get All URLs on a Page
- Practical Implementation of MSMQ Using ASP.Net and SQL Server
- Easiest way to implement Ajax in your ASP.Net 2.0 applications
- Rating and labelling your ASP.NET website
- Determining Whether an Email Account Exists
- Tuning Ubuntu on a Macintosh
- Edit and Encrypt Web.Config sections using C# 2.0
- Creating the Ajax application with Java
- Portfolio Website Design
- Create beautiful marble with Photoshop
- Design a Colorful USB Flash Stick with Tatto Stype Decals
- Add a wood bridge
- Blur picture effect with zoom in Flash
- 3D Vase with Illustrator CS2
- How to draw Theatrical Scene Wallpaper using Cute Animal Icons
- Design a Classic Muscle Car Concept Depicting Horse Power
- Placing Multiple Images Inside Text
- photoshop tutorial abstract light rays
- The Dark Knight Grunge Wallpaper
Login
Friends' Sites
Contact Us
Categories
- 3D
- ASP
- C#
- CSS
- Database
- Flash
- GIMP
- Hosting
- Illustrator
- Java
- Javascript
- Linux
- Photoshop
- PHP
- Web Design
- Windows

5,351 views
2 Comments
(7 votes, average: 4.86 out of 5)
2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]