Doug Reilly's Weblog

Embedded Reporting of the Information Age...

ASP.NET Page Title

Fabrice writes about makeing the Title as a server-side control, and dynamically changing it from code behind. This works well in general, but be prepared if using VS.NET to have the RunAt=Server attribute removed now and again (causing an error when you then reference the title in codebehind. Sigh. Hopefully, this will be resolved in Whitbey!

Comments

JW said:

Yes!!! This happens all of the time for me under VS.Net and VS.Net 2003. What an annoyance!

Why why why is this so!!! Fix it please MS.
# November 9, 2003 3:12 AM

Me said:

What I do for this (works as long as you don't dynamically add controls to your page) is:
<title><%=GetTitle()%></title>
# November 9, 2003 10:40 AM

Marco Garibaldi said:

Hi Doug,

I take another approach which in my opinion gives more flexibility. I have created a small server control, called PageHeader, which sets:

- the title
- stylesheet
- meta tags such as the page language, charset and keywords
- even the base href or favorite icon, when needed

You just add the control to the page this way:

<head>
<infonet:pageheader id="Head" runat="server" />
</head>

It works great and it's very useful when you need to localize info such as language code of the page or the keywords.

Here is the code:

Imports System.Web
Imports System.Web.UI
Imports Informa.Community.Logic.Components
Imports Informa.Community.Logic.Configuration

Public Class PageHeader : Inherits BaseForumControls
Private myDisplayTitle As Boolean

Public Sub New()
myDisplayTitle = True
End Sub


Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim fullTitle As String

With writer
If DisplayTitle Then
If CurrentContext.UserIsAuthenticated Then
fullTitle = String.Concat(CurrentContext.ForumName, ": ", Title, " - ", ResourcesManager.PageStyleLoggedInAs(CurrentContext.UserName, True))
Else
fullTitle = String.Concat(CurrentContext.ForumName, ": ", Title)
End If
End If

.RenderBeginTag(HtmlTextWriterTag.Title)
.Write(fullTitle)
.RenderEndTag()

.WriteLine()

' Set the base href
If Context.Request.Url.Host.ToLower <> "localhost" Then
.AddAttribute(HtmlTextWriterAttribute.Href, Context.Request.Url.GetLeftPart(UriPartial.Authority))
.RenderBeginTag(HtmlTextWriterTag.Base)
.RenderEndTag()
End If

.WriteLine()

' Add the style sheet
.AddAttribute("rel", "stylesheet")
.AddAttribute(HtmlTextWriterAttribute.Href, GlobalFiles.CSSFile())
.AddAttribute(HtmlTextWriterAttribute.Type, "text/css")
.RenderBeginTag(HtmlTextWriterTag.Link)
.RenderEndTag()

.WriteLine()

' Add the favicon link
.AddAttribute("rel", "shortcut icon")
.AddAttribute(HtmlTextWriterAttribute.Href, GlobalFiles.FavIcon())
.AddAttribute(HtmlTextWriterAttribute.Type, "image/x-icon")
.RenderBeginTag(HtmlTextWriterTag.Link)
.RenderEndTag()

.WriteLine()

' Add the meta tags
.AddAttribute("http-equiv", "Content-Language")
.AddAttribute("content", CurrentContext.CurrentCultureCode)
.RenderBeginTag(HtmlTextWriterTag.Meta)
.RenderEndTag()

.WriteLine()

.AddAttribute("http-equiv", "Content-Type")
.AddAttribute("content", "text/html; charset=utf-8")
.RenderBeginTag(HtmlTextWriterTag.Meta)
.RenderEndTag()

'TODO: add code for metatags
'Dim desc As String = Localization.LocalizeText(resourceKey, "MetaDescription, True)
'If desc.Length > 0 Then
' .AddAttribute(HtmlTextWriterAttribute.Name, "description")
' .AddAttribute("content", desc)
' .RenderBeginTag(HtmlTextWriterTag.Meta)
' .RenderEndTag()
'End If

'Dim keyWords As String = Localization.LocalizeText(resourceKey, "MetaKeywords, True)
'If keyWords.Length > 0 Then
' .AddAttribute("keywords", "keywords")
' .AddAttribute("content", keyWords)
' .RenderBeginTag(HtmlTextWriterTag.Meta)
' .RenderEndTag()
'End If
End With
End Sub


' Used to set the title of the page the control is rendered on
Public Property Title() As String
Get
Dim obj As Object = Me.ViewState("Title")
If Not obj Is Nothing Then
Return obj.ToString
Else
Return String.Empty
End If
End Get
Set(ByVal Value As String)
Me.ViewState("Title") = Value
End Set
End Property


Public Property DisplayTitle() As Boolean
Get
Dim obj As Object = Me.ViewState("DisplayTitle")
If Not obj Is Nothing Then
Return Convert.ToBoolean(obj)
Else
Return myDisplayTitle
End If
End Get
Set(ByVal Value As Boolean)
Me.ViewState("DisplayTitle") = Value
End Set
End Property

End Class
# December 23, 2003 9:18 AM

Nanda said:

This works good:

<title id="PageTitle" runat=server></title>

In the code behind:

Import:
Protected PageTitle As System.Web.UI.HtmlControls.HtmlGenericControl

page_load:
PageTitle.InnerText = "New Title"
# February 18, 2004 7:09 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)