Sign in
|
Join
Search
Doug Reilly's Weblog
Embedded Reporting of the Information Age...
Home
Contact
About
RSS
Atom
Comments RSS
Recent Posts
Tech Ed Lost and Found...
Are Software Development Books Obsolete?
If you are going to Tech Ed, Do Remember My Birds of a Feather...
CNET Article...
LiveStrong Challenge!
Tags
.NET Compact Framework
.NET Framework
ASP.NET
Random Rants
SQL Server
WinForms
Navigation
Home
Blogs
Affiliations
Blogs I Read
Jason Nadal's Weblog
The Scobleizer Weblog
G. Andrew Duthie's Blog
ScottW's ASP.NET Weblog
Julia Lerman Blog
My Other Blog (Mobile Software Developer's Log)
Database Related Links
Dr. Dobb's Database Forum
Archives
June 2006 (3)
May 2006 (1)
April 2006 (3)
March 2006 (5)
February 2006 (3)
January 2006 (2)
December 2005 (2)
November 2005 (4)
October 2005 (5)
September 2005 (4)
August 2005 (3)
July 2005 (5)
June 2005 (6)
May 2005 (6)
April 2005 (3)
March 2005 (3)
February 2005 (2)
January 2005 (2)
December 2004 (3)
November 2004 (1)
October 2004 (2)
September 2004 (4)
August 2004 (4)
May 2004 (2)
April 2004 (1)
March 2004 (5)
February 2004 (1)
January 2004 (3)
December 2003 (1)
November 2003 (5)
October 2003 (11)
September 2003 (2)
August 2003 (4)
July 2003 (2)
June 2003 (2)
May 2003 (3)
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!
Posted:
Nov 08 2003, 09:41 PM
by
douglas.reilly
| with
4 comment(s)
Filed under:
ASP.NET
,
.NET Framework
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
Title
(required)
Name
(required)
Your URL
(optional
)
Comments
(required)
Remember Me?