Dynamically changing the title of a webform in ASP.NET 1.1

If you started using ASP.NET 2.0, you'll probably know that you can set the title of a page dynamically using the new Title property. In ASP.NET 1.x, there is no straight forward way of doing it. One of the very common ways of achieving this is by placing a literal control between the title tags, and setting the text property of this literal control when you want to change the title. This way works fine, but lets say you are updating a web application that contains a big number of asp.net forms, and you need to set the titles of the webforms dynamically. Opening each form and adding this literal control to the head section of the page is just.....a pain in the neck.

So, what I did was, I came up with a way that made it easier to change the titles of webforms dynamically.

In most of the web applications I develop, I create a BasePage class that inherit's from the Page class, and make my webforms inherit from this BasePage class. I use it to add functionality that is common to all the webforms in the application.

The first thing I did is that I added a Title property to the BasePage class, this property will hold the title of the page.

  public string Title
  {
   get
   {
    
    if (this.ViewState["Title"]==null)
     return null;
    return (string)this.ViewState["Title"];
   }
   set
   {
    this.ViewState["Title"]=value;
   }
  }
  
  
Next, In the Pre_Render event of the Page class, I wrote the logic that changed the title of the page to the text that’s held in the Title property.

  private void BasePage_PreRender(object sender, System.EventArgs e)
  {
   
   if (this.Title !=null)
   {
    string tempMarkup = ((System.Web.UI.LiteralControl)this.Controls[0]).Text.ToLower();
    int startIndex = tempMarkup.IndexOf("<title>");
    int endIndex = tempMarkup.IndexOf("</title>");
    int length = endIndex - startIndex;
    string oldTitle = tempMarkup.Substring(startIndex,length);
    string newTitle = "<title>" + this.Title ;

    tempMarkup = tempMarkup.Replace(oldTitle,newTitle);
    ((System.Web.UI.LiteralControl)this.Controls[0]).Text = tempMarkup;
   }

  }  
  
What I did is the following:
I read the text of the first control in the control tree (this control usually holds the static html markup that appears at the top of the webform). I then parsed through the text to find the title attribute, replaced it with the text from the Title property, and then put the text back where I got it from (the literal control).

There you go, now all the webforms that inherit from the BasePage class will have a Title property.

It’s not the best solution, but it’s a solution that works, and it saves a lot of time, especially if you have a lot of webforms in your web application.

You can download the BasePage class from here. (It has a couple of other methods that I usually use)

Published Wednesday, January 18, 2006 4:32 AM by admin
Filed Under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Dynamically changing the title of a webform in ASP.NET 1.1 @ Thursday, January 19, 2006 4:11 AM

Hi Wessam,
I must admit it is a nice and tricky way of controlling the Page title in an ASPX page.

I would like to add my 2 cents for the BasePage_PreRender method, by implementing it using Regular Expression for the string manipulation:

private void BasePage_PreRender(object sender, System.EventArgs e)
{
if (this.Title !=null)
{
string tempMarkup = ((System.Web.UI.LiteralControl)this.Controls[0]).Text.ToLower();
tempMarkup = Regex.Replace(tempMarkup, @"<title[^>]*>(.*?)</title>", @"<title>" + this.Title + @"</title>");
((System.Web.UI.LiteralControl)this.Controls[0]).Text = tempMarkup;
}
}

Both work fine!!

Thanks,

Bilal Haidar [MVP]

# re: Dynamically changing the title of a webform in ASP.NET 1.1 @ Thursday, January 19, 2006 4:28 AM

Good work Bilal. I actually had regular expressions in mind, but I was to lazy to do it.

Thanks :)

admin

What do you think?

(required) 
required 
optional
required