|
Home Preview the Book Chapter 15, TOC Part 15
Previous:
Developing a Control for the Navigation Menu
Next:
Embedding a Custom Control in a User Control
Rendering the Navigation HTML
The custom control in Listing 15-5 uses the value of its CurrentIndex property to control the rendering of its HTML. Because the control resides in a separate DLL, it also requires a ResourceManager instance from its caller to provide the desired localization.
Listing 15-5. The Navigation.vb Custom Control
Option Strict On
Imports System.Resources
Imports System.ComponentModel
Imports System.Web.UI
<DefaultProperty("CurrentIndex"), _
ToolboxData("<{0}:Navigation runat=server></{0}:Navigation>")> _
Public Class Navigation
Inherits System.Web.UI.WebControls.WebControl
Private _rm As ResourceManager
Private _asLink() As String = {"index.aspx", _
"code_search.aspx", "code_browse_all.aspx", _
"my_profile.aspx", "terms_of_use.aspx", "privacy_policy.aspx"}
Private _iCurrent As Integer = 0
<Bindable(True), Category("Behavior"), DefaultValue(0)> _
Property CurrentIndex() As Integer
Get
Return _iCurrent
End Get
Set(ByVal Value As Integer)
If Value <= UBound(_asLink) Then
_iCurrent = Value
End If
End Set
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-Navigation: " _
& "No resource manager provided.")
End If
Dim asLabel() As String = Split(_rm.GetString("NavStrings"), ",")
Dim i As Integer, iMax As Integer = UBound(asLabel)
If iMax <> UBound(_asLink) Then
Throw New Exception("C15_Ctl_Lib-Navigation: " _
& "Wrong number of localized navigation strings.")
End If
'Move to left margin
output.WriteLine()
For i = 0 To iMax
If i = 4 Then
'Insert separator line before next paragraph
output.AddAttribute(HtmlTextWriterAttribute.Align, "center")
output.RenderBeginTag(HtmlTextWriterTag.P)
output.AddAttribute(HtmlTextWriterAttribute.Border, "0")
output.AddAttribute(HtmlTextWriterAttribute.Width, "120")
output.AddAttribute(HtmlTextWriterAttribute.Height, "8")
output.AddAttribute(HtmlTextWriterAttribute.Alt, "")
output.AddAttribute(HtmlTextWriterAttribute.Src, _
"images/box_h8.gif")
output.RenderBeginTag(HtmlTextWriterTag.Img)
output.RenderEndTag()
output.WriteLine()
End If
'Insert link text paragraph
output.RenderBeginTag(HtmlTextWriterTag.P)
If i = 0 Then
'Add shim for spacing
output.AddAttribute(HtmlTextWriterAttribute.Width, "8")
output.AddAttribute(HtmlTextWriterAttribute.Height, "8")
output.AddAttribute(HtmlTextWriterAttribute.Alt, "")
output.AddAttribute(HtmlTextWriterAttribute.Src, _
"images/shim.gif")
output.RenderBeginTag(HtmlTextWriterTag.Img)
output.Write("<br />")
End If
If i = _iCurrent Then
'Already linked to this page (or section)
output.AddAttribute(HtmlTextWriterAttribute.Class, "navtopic")
output.RenderBeginTag(HtmlTextWriterTag.Span)
output.Write(asLabel(i))
output.RenderEndTag()
Else
'Output link
output.AddAttribute(HtmlTextWriterAttribute.Href, _asLink(i))
output.AddAttribute(HtmlTextWriterAttribute.Class, "nav1")
output.RenderBeginTag(HtmlTextWriterTag.A)
output.Write(asLabel(i))
output.RenderEndTag()
End If
If i = iMax Then
'Add spacing
output.Write("<br /> ")
End If
'Close paragraph
output.RenderEndTag()
output.WriteLine()
Next
End Sub
End Class
The CurrentIndex property for the navigation custom control uses a value of 0 for the homepage item, and the second level items start with a CurrentIndex value of 1. The aslink() string array contains the list of links for all of the Web pages in the control, with the list ordered to correspond to the associated CurrentIndex values. The text for each item comes from the NavStrings data in the resource file that’s determined by the ResourceManager instance. For English-speaking visitors, the NavStrings data appears in strings.resx like this:
<data name="NavStrings">
<value>Home,Search for Code,Browse Code,My Profile,Terms of Use,Privacy
Policy</value>
</data>
Each of the other supported languages has its own NavStrings data.
The Render method outputs links for all of the navigation menu items except for the item corresponding to the CurrentIndex property. The Render method outputs a span element for the CurrentIndex item. CSS classes supplied by the Web designer control the appearance of the elements within the control. The Render method also inserts a graphical separator after the first four items.
The actual HTML rendered by the navigation custom control matches that supplied by the Web designer for each page. By looking at the HTML delivered by the Web designer for each page, it was a simple task to see the pattern. Testing the control consisted of comparing the HTML rendered by the control with that in the original design for each Web page.
One of the great things about custom controls is that you can render any HTML specified by the Web designer. In tough situations, this can be a lifesaver.
Previous:
Developing a Control for the Navigation Menu
Next:
Embedding a Custom Control in a User Control
Home Preview the Book Chapter 15, TOC Part 15
|