|
Home Preview the Book Chapter 15, TOC Part 19
Previous:
Deciding What Breadcrumb Links to Display
Next:
Using Session State to Remember Previous Links
Implementing a Hybrid Breadcrumb Link Design
Most of the VB Snippets Web pages display hierarchical breadcrumb links. When it comes to the display of code examples, though, the breadcrumb links make it easy for the visitor to return to the previous search page, browse page, or homepage.
Listing 15-7 contains the code for the breadcrumbs custom control. Like the navigation custom control, the breadcrumbs custom control exposes a ResourceManager property that’s used for localization purposes. The CurrentPage property identifies the last breadcrumb in the chain—the breadcrumb that is not a link. The BrowseItem property identifies the specific keyword to display in a browse breadcrumb link.
(Figure 15-12 illustrates how the BrowseItem property is used.) The From property identifies the route the visitor took to arrive at the current page. VB Snippets uses session-state variables to store this information. The read-only CodePath property returns the index used by the code_example page to control the navigation menu display. If the visitor arrived via a search page, for example, the CodePath property returns the value 1.
Listing 15-7. The BreadCrumbs.vb Custom Control
Option Strict On
Imports System.Resources
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web
<DefaultProperty("CurrentPage"), _
ToolboxData("<{0}:BreadCrumbs runat=server></{0}:BreadCrumbs>")> _
Public Class BreadCrumbs
Inherits System.Web.UI.WebControls.WebControl
Private _rm As ResourceManager
Private _sCrumbList As String = "Home,"
Private _sLinkList As String = "index.aspx"
Private _sCurrentPage As String
Private _sFrom As String
Private _iCodePath As Integer = 0
Private _sBrowseItem As String = String.Empty
<Bindable(True), Category("Behavior"), DefaultValue("")> _
Property CurrentPage() As String
Get
Return _sCurrentPage
End Get
Set(ByVal Value As String)
'Values: Browse, Search, Code, Terms, Privacy
_sCurrentPage = Value
Select Case Value
Case "Browse"
'Browse all unless BrowseItem set
If _sBrowseItem = String.Empty Then
_sCrumbList &= "BrowseAll"
End If
Case "Search"
'General search unless SearchQuery set
_sCrumbList &= "SearchFor"
Case "Results"
_sCrumbList = "Home,SearchFor,Results"
_sLinkList = "index.aspx,code_search.aspx"
Case "Code"
'Set default
_sCrumbList &= "Code"
'Code breadcrumbs depend upon visitor's navigation
If Not _sFrom = String.Empty Then
If _sFrom = "B" Then
'Got here from Browse All
_sCrumbList = "Home,BrowseAll,Code"
_sLinkList = "index.aspx,code_browse_all.aspx"
_iCodePath = 2
ElseIf _sFrom.StartsWith("B:") Then
'Got here from Browse Item
_sCrumbList = "Home,BrowseAll,Browse,Code"
_sBrowseItem = _sFrom.Substring(2)
_sLinkList = "index.aspx,code_browse_all.aspx,code_browse_" _
& _sBrowseItem & ".aspx"
_iCodePath = 2
ElseIf _sFrom.StartsWith("S:") Then
'Got here from Search Results
_sCrumbList = "Home,SearchFor,Results,Code"
_sLinkList _
= "index.aspx,code_search.aspx,code_search_results.aspx"
_iCodePath = 1
Else
'Retain defaults
End If
End If
Case Else
'Homepage only link
_sCrumbList &= Value
End Select
End Set
End Property
<Bindable(True), Category("Behavior"), DefaultValue("")> _
Property BrowseItem() As String
Get
Return _sBrowseItem
End Get
Set(ByVal Value As String)
'Specific browse
_sBrowseItem = Value
_sCrumbList = "Home,BrowseAll,Browse"
_sLinkList = "index.aspx,code_browse_all.aspx"
End Set
End Property
<Bindable(False)> _
Property From() As String
Get
Return _sFrom
End Get
Set(ByVal Value As String)
_sFrom = Value
End Set
End Property
<Bindable(False)> _
ReadOnly Property CodePath() As Integer
Get
Return _iCodePath
End Get
End Property
<Bindable(False)> _
Property ResourceManager() As ResourceManager
Get
Return _rm
End Get
Set(ByVal Value As ResourceManager)
_rm = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
If _rm Is Nothing Then
Throw New Exception("C15_Ctl_Lib-BreadCrumbs: " _
& "No resource manager provided.")
End If
Try
Dim asCrumb() As String = Split(_sCrumbList, ",")
Dim asLink() As String = Split(_sLinkList, ",")
Dim i As Integer, iMax As Integer = UBound(asCrumb)
'All except last crumb are links
Debug.Assert(iMax = UBound(asLink) + 1, "C15_Ctl_Lib-BreadCrumbs: " _
& "Mismatch in list counts.")
'Crumbs: Home, BrowseAll, Browse, SearchFor, Results,
' Code, Terms, Privacy
For i = 0 To iMax
If asCrumb(i) = "Browse" Then
asCrumb(i) = _rm.GetString("Browse") _
& " (" & _sBrowseItem & ")"
Else
asCrumb(i) = _rm.GetString(asCrumb(i))
End If
Next
'Move to left margin
output.WriteLine()
'Render breadcrumbs paragraph
output.AddAttribute(HtmlTextWriterAttribute.Class, "lfloatpad")
output.RenderBeginTag(HtmlTextWriterTag.P)
For i = 0 To iMax
'Render arrow image
output.AddAttribute(HtmlTextWriterAttribute.Border, "0")
output.AddAttribute(HtmlTextWriterAttribute.Width, "6")
output.AddAttribute(HtmlTextWriterAttribute.Height, "10")
output.AddAttribute(HtmlTextWriterAttribute.Alt, "")
output.AddAttribute(HtmlTextWriterAttribute.Src, _
"images/arrow.gif")
output.RenderBeginTag(HtmlTextWriterTag.Img)
If i < iMax Then
'Create a link
output.AddAttribute(HtmlTextWriterAttribute.Class, "crumb")
output.AddAttribute(HtmlTextWriterAttribute.Href, asLink(i))
output.RenderBeginTag(HtmlTextWriterTag.A)
output.Write(asCrumb(i))
output.RenderEndTag()
Else
'Display current page info
output.AddAttribute(HtmlTextWriterAttribute.Class, "crumb")
output.RenderBeginTag(HtmlTextWriterTag.Span)
'If Not _sBrowseItem = String.Empty Then
' asCrumb(i) &= " (" & _sBrowseItem & ")"
'End If
output.Write(asCrumb(i))
output.RenderEndTag()
End If
Next
output.RenderEndTag()
'Render log in/out link
output.AddAttribute(HtmlTextWriterAttribute.Class, "rfloatpad")
output.RenderBeginTag(HtmlTextWriterTag.P)
output.AddAttribute(HtmlTextWriterAttribute.Class, "logout")
If Page.User.Identity.IsAuthenticated Then
'Already logged in
output.AddAttribute(HtmlTextWriterAttribute.Href, _
"index.aspx?action=logout")
output.RenderBeginTag(HtmlTextWriterTag.A)
output.Write(_rm.GetString("LogOut"))
output.RenderEndTag()
ElseIf _sCurrentPage <> "LogIn" Then
'Link to log in page
output.AddAttribute(HtmlTextWriterAttribute.Href, "log_in.aspx")
output.RenderBeginTag(HtmlTextWriterTag.A)
output.Write(_rm.GetString("LogIn"))
output.RenderEndTag()
End If
output.RenderEndTag()
output.RenderEndTag()
Catch ex As Exception
Throw New Exception("C15_Ctl_Lib-BreadCrumbs: " & ex.Message)
End Try
End Sub
End Class
The breadcrumbs custom control works by building two lists: one for the text to display for each breadcrumb and another list for the link associated with each breadcrumb except the last, which represents the current page. The control stores the lists in the string variables _sCrumbList and _sLinkList until the Render method needs them. The Render method splits the strings into string arrays named asCrumb() and asLink() for processing.
The values in asCrumb() serve as keys to resource files to obtain localized strings. The Render method creates the appropriate link for each breadcrumb except for the last one in the asCrumb() array.
Previous:
Deciding What Breadcrumb Links to Display
Next:
Using Session State to Remember Previous Links
Home Preview the Book Chapter 15, TOC Part 19
|