Setting the Page Title from a Server Control

Earlier this week, I was working on a project and needed to find a way to set the title for the page from a control that was loaded programmatically onto an aspx page that was using a MasterPage. I knew how to set the page title from the child of the MasterPage (by adding it to the @Page definition), but that wasn't going to work. I needed a way to set that from a server control that was going to be loaded during the Page_Load event.

Here's the structure I was working with:

Article.aspx is a page that displays certain articles, based on the id passed in the QueryString. The Article.aspx page references main.master that provides the site layout.

In the Page_Load event in Article.aspx, the server control that actually renders the article is loaded with LoadControl(..) and the control is added to the placeholder on Article.aspx.

I wanted all the data access to happen in the control. I didn't want to have to make a call to the database from Article.aspx just to retrieve the title of the article. So I turned to Google as I often do. One of the first pages I came across was, not surprisingly, from 4 Guys From Rolla (http://aspnet.4guysfromrolla.com/articles/051006-1.aspx). I have found myself on the 4 Guys site many times and I usually come up with a great solution. It is a great reference site. But anyway, the article seemed to have exactly what I needed. I read through it and was a little dismayed. I was hoping for something a little more elegant than putting a runat=server in the HEAD tag of the master page.

The next page I visited was on .NET 247 (http://www.dotnet247.com/247reference/msgs/23/117737.aspx), another great reference site, although personally I get frustrated with the layout. This had another solution to pass variables between pages, but it was more complicated than I wanted as it was for a much different purpose.

After reading through a few more pages, I pieced together a solution. The control has a reference to the parent page, via this.Page. So after just trying it out, I found that the control could set the page title all the way up the chain with 

this.Page.Title = pageArticle.Title;

So I had my problem solved. I could go to the database just once to retrieve the article from the control where it made the most sense. I didn't have to jump through any hoops like exposing properties on the various pages and controls to get the data back and forth or having the head of the parent page become a server control. After quite a bit of digging, I was actually led to something that seemed so simple, I should have known it off the top of my head.

I was happy for a bit, until i realized that I wanted my site's name in the Title to, for SEO reasons. I could add that to the control, but that would sort of defeat the purpose of the "control" as it would now be bound to that site. So I turned to the master page, where it seemed most beneficial to have code that should be site-wide. I added a handler for the Page_PreRender event and added the following:

private void Page_PreRender(object sender, EventArgs e)
{
    if (!Page.Title.Contains("Web Security Tips"))
    {
        Page.Title += " - Web Security Tips";
    }
}

Now I've got my site's name in the Title of every page and I can set the rest of the title from whichever control I'm using to retrieve data for the current page.

I hope this helps. I know it isn't rocket science that I solved here, but sometimes the most obvious solutions can elude a developer when they are drilled into the process.

Trent Grandey

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

January 5. 2009 22:26

Gravatar