Friday, September 7, 2007

Using BreadCrumbs in ASP.Net.

The BreadCrumb is used to maintain the list or keep trail of the pages that one has visited along the way to the present page.
As the breadcrumb is used in the entire project ,so use it as a usercontrol.
Following steps are needed to create a BreadCrumb:-
1)Create a usercontrol and paste the following code in the .ascx.vb page:-
' This class represents a usercontrol helps to implement a breadcrump system
' Usage : Ad the user control to the page where bread crumb system to be implemented
' Add this page tro the DefaultLinks property if it not exiss there
Imports System.Text
Imports System.Collections
Imports System.Collections.Specialized
Public Class BreadCrumb
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
Private Sub InitializeComponent()

End Sub
Protected WithEvents lblTrail As System.Web.UI.WebControls.Label

'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

'Variable holding the Link name of the page
Private _tailName As String
'Variable holding the level of the page
Private _level As Short
'The pagecrumb object of the current page
Private _pageCrumb As New BreadCrumb
'We will use a sorted list as we can use the level as key
Private _crumbList As SortedList

'Each page has a level. The page should declare its level
Public Property Level() As Short
' TO DO : We can check for some constraints here
Get
Return _level
End Get
Set(ByVal Value As Short)
_level = Value
End Set
End Property

Public Structure BreadCrumb

Private _level As Short
Private _url As String
Private _linkName As String

'//We are setting all the properties at the time of construction

Public Sub New(ByVal level As Short, ByVal url As String, ByVal linkName As String)

_level = level
_url = url
_linkName = linkName
End Sub

'//We are making all the properties as read-only.
'//We are not expecting it to change once it is set.
Public ReadOnly Property Level() As Short
Get
Return _level
End Get
End Property

Public ReadOnly Property Url() As String
Get
Return _url
End Get
End Property

Public ReadOnly Property LinkName() As String
Get
Return _linkName
End Get
End Property
End Structure


'Each page needs a meaningful name of it. Let them declare it
Public Property TailName() As String
' TO DO : We can check for some constraints here
Get
Return _tailName
End Get
Set(ByVal Value As String)
_tailName = Value
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'We are not disabling viewstate
If Not (Page.IsPostBack) Then

'Minimum level is 1
If (_level <= 0) Then
_level = 1
End If

'If no friendly name gives Untitled as default
If (_tailName = "") Then
_tailName = "Untitled"
End If

'Create a Crumb object based on the properties of this page
_pageCrumb = New BreadCrumb(_level, Request.RawUrl, _tailName)

'Check our Crumb is there in the session...if not create and add it...else get it
If Session.Item("HASH_OF_CRUMPS") Is Nothing Then
_crumbList = New SortedList
Session.Add("HASH_OF_CRUMPS", _crumbList)
Else
_crumbList = Session.Item("HASH_OF_CRUMPS")
End If

'Now modify the List of the breadcrumb
ModifyList()
' Put the breadcrumb from the session of sortlist
PutBreadCrumbs()
End If


End Sub

Private Sub ModifyList()
'Remove all Entries from the list which is higher or equal in level
'Because at a level there can be max 1 entry in the list
RemoveLowerLevelCrumbs()
'If level is 1 set the Crumb as home
If _pageCrumb.Level = 1 Then
_crumbList.Clear()
_crumbList.Add(CType(1, Short), New BreadCrumb(1, "/Home.aspx", "Home"))

Else
'If nothing in the list adds the home link first
If _crumbList.Count = 0 Then
_crumbList.Add(CType(1, Short), New BreadCrumb(1, "/Home.aspx", "Home"))
End If
'Now add the present list also no other check is required here as we have cleaned up the
'List at the start of the function
_crumbList.Add(_level, _pageCrumb)
End If
End Sub

'Function will remove all the entries from the list which is higher or equal to the
'present level
Private Sub RemoveLowerLevelCrumbs()

Dim level As Short
Dim removalList As New ArrayList(_crumbList.Count)
For Each level In _crumbList.Keys
If (level >= _level) Then
removalList.Add(level)
End If
Next
'Now remove all keys in the list
For Each level In removalList
_crumbList.Remove(level)
Next
End Sub


Private Sub PutBreadCrumbs()
Dim linkString As New StringBuilder

Dim pageCrumb As New BreadCrumb
Dim index As Integer

For index = 0 To _crumbList.Count - 2
pageCrumb = _crumbList.GetByIndex(index)
linkString.Append(String.Format("{1} ", pageCrumb.Url, pageCrumb.LinkName))
linkString.Append(" > ")
Next index
'Add the tail also
pageCrumb = _crumbList.GetByIndex(index)
linkString.Append(pageCrumb.LinkName)

lblTrail.Text = linkString.ToString()

End Sub
End Class

Now add a label which will display the pages .
So the html part of the usercontrol for the label is:-


Now use the following in the aspx pages which needs the BreadCrumb:-


The level and the tailname changes according to the page that is to be displayed.
So better prepare a sitemap first.

Hope this article will provide a help to you.

Happy Coding,
Sumit Daripa

19 comments:

Anonymous said...

Good post, adding it to my blog now, thanks. :)

Anonymous said...

My gf and I were sent here simply because this particular article was tweeted by a girl I had been following and feel very I made it here. scholarships

Anonymous said...

Thanks for sharing us informative entries.

Anonymous said...

You nicely summed up the issue. I would add that this doesn’t exactly concenplate often. xD Anyway, good post…

Anonymous said...

Great Info! But I’m having some trouble trying to load your blog. 2011 scholarships

Thanxx

Anonymous said...

I couldn t agree more! well done! financial help

financial help

Anonymous said...

Thanks for sharing us informative ideas.

thanxx


newegg promo code

Anonymous said...

Hello

Awesome blog, great write up, thank you!

Anonymous said...

Hello

Thanks for sharing, I have digged this post

Anonymous said...

Sorry for the stupid question. What is the best search engine http://google.com or http://yahoo.com?

Anonymous said...

Heya
I truly desire some fantastic brand new movies. I wish to buy some a new one.
I like crazy, scary, and also actions and loving providing these are really efficient.
; ) consequently fundamentally some good innovative dvds.
Thank you

Anonymous said...

Hey - I am really delighted to find this. great job!

Anonymous said...

I'm sure the best for you windows registry repair suprisely

Anonymous said...

Hi

I'm lookingo for people who like strength sports eg. bodybuilding. You can take part in a huge project of testing sport supplements.
We will be test supplements like this
[url=http://www.trec-gainery.pl]Gainery Trec[/url] If you are interested in this project please send me information on e-mail: bodybuildingprojectone@gmail.com
We will contact only with chosen people

Sumit Daripa said...

Guys,

Please post the comments related to an article under the comment section of that particular article.

Anonymous said...

Hi

What do you thing about below diet supplement? I'm going to buy something good for muscle growth. Please give me a piece of advice.

[url=http://www.suplementy-olimp.pl/olimp-gainer-mct.php]Gainer[/url]

Sumit Daripa said...

Whey protein is the best....

Anonymous said...

Sorry i have not commented till now, i have been reading your blog for a whilst now though. It has become part of my morning routine.

Anonymous said...

My Person really just stumbled on this web site and of course read various articles.
I fairly like for instance the style of writing and just
how each of the texts are formatted. I bookmarked your post so I can find that
site faster later. Well I would like to explain to you a bit about our internet site, that is extremely
relevant to your personal text. Without a doubt it is
on the topic of maize food and also its risks and advantages that it can have for the human body.
Also, you can discover guides that have a discussion
about diet programs lacking corn, as well as meaningful for people suffering from
an allergic reaction. I be aware of that The layout
isn’t the highest quality, then again I know that the expertise is in depth
and legitimate. I believe that can be the most important
part associated to a website. You are going to so many
blogs that look really good, but then do not have
virtually any advice on it. From time to time a part of
several given advise can be unsuitable. This is often threatening regarding
healthcare associated resources. Well, possibly we are able
to remain in contact. I am going to visit your
web site on a regular basis, so remember to never stop the postings.
English is not actually my number 1 language,
I finally hope you could maybe understand the statement.
Look at my web site ... corn nuts nutrition