Common page titles for child pages in ASP.NET app

In almost all my application I utilize master pages and in most cases my master page has two content place holders: one for the child page content and one for the child page heading content. This helps me give the application a consistent look and feel. I like having a naming convention for the pages in my app. Usually "[App Name]" for the default page and then "[App Name] - [Function Name]" for the other pages. For example, if I have "Production Forecast Tool" as the name of my app, then the admin menu page would have a name of "Production Forecast Tool - Admin Menu". I had been setting the page title separately for each page but then had one application where I needed to change the name and it was a pain to edit all the entries. I realized my page heading content had a label with the function name I wanted to included in my page title so I just added a few lines of code to my master page's load even to take of the page titles for me.

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   If Not IsPostBack Then

      Dim lblHeading As Label = Me.SubHeading.FindControl("lblHeading")

      If Not (lblHeading Is Nothing) Then

         Me.Page.Title = "Production Forecast Tool (PFT) - " & lblHeading.Text

      Else

         Me.Page.Title = "Production Forecast Tool (PFT)"

      End If

   End If

End Sub

The caveat is that all the child pages must have the same control name for the heading label. Now if the application name needs to change I need only change it once in my master page. Simple but effective.

Published Monday, December 08, 2008 11:08 AM by bunbun
Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)