Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

ASP.NET - Dynamic titles

Great tip from Ashutosh Nilkanth to programatically change the title of an ASP.NET page:

As I learnt today, the title (browser window title) of an ASP.NET page can be changed programatically. Here how ...

In the HEAD section of the ASP.NET page (.aspx) define the title as a server side control:

... and in the code-behind (.aspx.vb) or inline code, define ...

Protected PageTitle As New HtmlGenericControl

... and set the title from anywhere in the code as ...

Me.PageTitle.InnerText = "Hello World"

A cool technique to implement for a template-driven UI.

I used to do this by other ways such as Master Pages or string replacements, but this is very clean.

This technique works well for CSS links too, you just have to close the LINK tag.

Comments

TrackBack said:

Problem with Visual Studio
# November 13, 2003 7:18 AM

TrackBack said:

<a href="http://weblogs.asp.net/fmarguerie/archive/2003/11/09/36546.aspx">Fabrice's weblog</a> gave me the quick and easy answer to how to do this. The only problem I ran into ...
# January 7, 2004 5:39 AM

Chris Scott said:

Thanks, this works well. Unfortunately, VS.NET (at least 2002) likes to take the runat="server" attribute off the tag when you switch from code to HTML view.
# January 7, 2004 11:35 AM

Scavenger said:

Pretty cool... This is one I'll use.
# January 29, 2004 6:20 PM

Me=Dumba55 said:

Ummm... i don't quite understand all this.
what do i have to put in the html? please give me cut-and-paste code =)
# May 4, 2004 9:03 AM

Fabrice said:

<title id="PageTitle" runat=server></title> should do it.
# May 5, 2004 12:15 PM

wlg said:

<title>
<%=myTitle%>
</title>


will work just as well, also in 2002 version
# May 11, 2004 10:05 AM

Frank said:

but It works when I put the all the code in the same page

if I try the code-behind , it does not work

looks like the server think the
PageTitle are 2 different variables in the
aspx , and aspx.vb page
# May 19, 2004 1:24 AM

Randhir said:

Definitely there a better alternative, use the placeholder tag.
<HTML>
<HEAD>
<asp:PlaceHolder id="titlePlaceHolder" runat="server" />
</HEAD> <BODY>
...
...
...
</BODY></HTML>

In the codebehind
private void Page_Load(object sender, System.EventArgs e)
{
PageTitle= new System.Web.UI.HtmlControls.HtmlGenericControl("title");
titlePlaceHolder.Controls.Add(PageTitle);
PageTitle.InnerHtml="Whatever title you want!";

}
# June 22, 2004 6:40 PM

Fabrice said:

Note that this is supported in ASP.NET 2: in a page, you can use Header.Title
# June 22, 2004 6:45 PM

Krishna said:

But would this work even though the page title placeholder (runat=server control) is NOT within the FORM tag?, coz usually the form tag is in the body...right?
# July 1, 2004 1:37 PM

Fabrice said:

It works for title, I guess it would work with the PlaceHolder.
# July 1, 2004 4:00 PM

Randhir said:

Yes it works even though placeholder is ouside the form
# July 4, 2004 9:24 PM

Darushin said:

Thank you for the place holder code. It was clean and works excellent.
# July 27, 2004 5:19 PM

avinash said:

Note that this is supported in ASP.NET 2: in a page, you can use Header.Title.

It just doesn't change the Title.

# February 13, 2007 10:53 AM

David Boulet said:

You can also just use a literal tag placed in the inner-text area of the HTML title tag, like this. This is probably the simplest of all solutions:

(HTML)

<title><asp:literal id="litTitle" runat="server" ></asp:literal></title>

(code behind)

litTitle.Text = "My Web Page Title!"

# March 12, 2007 11:02 AM

Fabrice Marguerie said:

David, as of .NET 2, the simplest solution is to use the Page.Title property.

# March 12, 2007 1:04 PM

Charlie said:

<title><asp:literal id="litTitle" runat="server" ></asp:literal></title>

Will not work..at least that is true in VS.Net 2005 Pro with sp1. It will failed during compliation.

However, I did that using Head1 is the easiest for me.

<head id="Header" runat="server">

Then in code behind:

Header.Title = "whatever it title you want";

# May 25, 2007 12:33 PM

Aji Murugan said:

****** There is no confusion! with .net 2.0

Header.Title = "Aji's Page"

# December 14, 2007 9:11 AM

boot_dev said:

How do you change the Keywords and Description using 2.0 vs05sp1?

# April 3, 2008 3:48 PM

Luciano Ferrarez said:

In my .vb page:

Me.Page.Title = "Luciano Brasil

# June 9, 2008 3:15 PM

Fred said:

Me.Page.Title = "Luciano Brasil" will not work...

If for example you are using master pages then use the following (VB)...

Dim _masterPageTitle As HtmlTitle            

_masterPageTitle = CType(Master.FindControl("PageTitle"), HtmlTitle)

masterPageTitle.text = "WOO HOO!"

i.e. declare it not as a generic html control but instead as a type htmltitle control i.e. what it actually is!

# October 13, 2008 12:06 PM

rahel_kareem said:

quite easy, add a runat="server" to head:

<head runat="server" runat="server">

then:

Page.Header.Title = "www.hrtribune.net"

# April 16, 2009 3:44 PM

Mohamed Imran said:

@rahel kareem

Thanks rahel!

its so simple....

# September 28, 2009 4:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)