ASP.NET Hosting

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.

22 Comments

  • 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.

  • Pretty cool... This is one I'll use.

  • Ummm... i don't quite understand all this.

    what do i have to put in the html? please give me cut-and-paste code =)

  • <title id="PageTitle" runat=server></title> should do it.

  • <title>

    <%=myTitle%>

    </title>





    will work just as well, also in 2002 version

  • 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

  • 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!";



    }

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

  • 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?

  • It works for title, I guess it would work with the PlaceHolder.

  • Yes it works even though placeholder is ouside the form

  • Thank you for the place holder code. It was clean and works excellent.

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

    It just doesn't change the Title.

  • 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)




    (code behind)

    litTitle.Text = "My Web Page Title!"

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



  • 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.



    Then in code behind:
    Header.Title = "whatever it title you want";

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

    Header.Title = "Aji's Page"

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

  • In my .vb page:

    Me.Page.Title = "Luciano Brasil

  • 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!

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

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

  • @rahel kareem

    Thanks rahel!

    its so simple....

Comments have been disabled for this content.