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 "
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
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
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
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"}
Dim tmpPath As String = Mid(FullPath, 1, InStr(1, FullPath, "/") - 1)
Dim strTmpPath As String = Trim(tmpPath)
Dim DirPath As String = DirPath & strTmpPath & "/"
Dim firstLetter As String = UCase(Mid(strTmpPath, 1, 1))
strTmpPath = firstLetter & Mid(strTmpPath, 2, Len(strTmpPath))
For Each letter As String In Letters
strTmpPath = Replace(Trim(strTmpPath), "_" & LCase(letter), " " & UCase(letter))
Next
FullPath = Mid(FullPath, InStr(1, FullPath, "/") + 1, Len(FullPath) - Len(tmpPath))
If strTmpPath = "" Then
MyCrumb.Append("<a href=""/"" style=""text-decoration:none"">Home Page</a>")
ElseIf strTmpPath = "Home" Then
Else
MyCrumb.Append(" > <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
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):
|
<%@ 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>