Breadcrumb

This is a useful trick to write a breadcrumb in any web page, like:

Home Page > Breadcrumbs : About Us

I wrote this code in few minutes so feel free to comment if you see some improvements to be done !

You need to write this code behind in an ascx file as a control


Imports System.Text


Public Class crumb
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Public PageTitle As String
Public Bread As PlaceHolder

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Bread.Controls.Add(New LiteralControl(BreadCrumb(Request.ServerVariables("PATH_INFO"))))
End Sub
Function BreadCrumb(ByVal FullPath)
Dim MyCrumb As New StringBuilder

Do Until InStr(1, FullPath, "/") = 0

'## Create an array of letters in the alphabet.
Dim Letters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

'## split on the /
Dim tmpPath As String = Mid(FullPath, 1, InStr(1, FullPath, "/") - 1)
Dim strTmpPath As String = Trim(tmpPath)
Dim DirPath As String = DirPath & strTmpPath & "/"

'## upshift the first character
Dim firstLetter As String = UCase(Mid(strTmpPath, 1, 1))
strTmpPath = firstLetter & Mid(strTmpPath, 2, Len(strTmpPath))

'## replace udnerscores with spaces and upshift the following character
For Each letter As String In Letters
strTmpPath = Replace(Trim(strTmpPath), "_" & LCase(letter), " " & UCase(letter))
Next

'## split the next one out
FullPath = Mid(FullPath, InStr(1, FullPath, "/") + 1, Len(FullPath) - Len(tmpPath))

'## separate them with >> symbols
If strTmpPath = "" Then
MyCrumb.Append("<a href=""/"" style=""text-decoration:none"">Home Page</a>")
ElseIf strTmpPath = "Home" Then
Else
MyCrumb.Append(" &gt; <a href=""" & DirPath & """ style=""text-decoration:none"">" & strTmpPath & "</a>")
End If
Loop

If PageTitle = "" Then
MyCrumb.Append(" : Current Page")
Else
MyCrumb.Append(" : " & PageTitle)
End If

Return MyCrumb.ToString
End Function
End Class



<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="breadcrumbs.WebForm1"%>

<%@ Register TagPrefix="uc1" TagName="crumb" Src="crumb.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<uc1:crumb id="Crumb1" pagetitle="About Us" runat="server"></uc1:crumb>


</form>
</body>
</HTML>

And just implement the control in your page where you want to have your Breadcrumb something like (don't forget to declare the page Title in the control tag):

2 Comments

Comments have been disabled for this content.