Working with the page title in ASP.NET


Recently, I’ve had a chance to work with ASP.NET 2.0 a bit, but I still don’t have a business reason to really dive in. One thing I have noticed with the next version is more access to page elements. One particular feature is access to a page’s title, so it can easily be changed in code. I’ve often wanted to do this and I usually utilize pass-thru code to accomplish something dynamic.

Thankfully, it can be accomplished in the current version of ASP.NET. It is easy enough by setting the page title tag as a server control (i.e. with a
runat=”server”) and utilize the following in your codebehind or on the page:

Protected pageTitle As System.Web.UI.HtmlControls.HtmlGenericControl
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
pageTitle.InnerText = "Title"
End Sub

This is VB.NET but it is easily translated to C#.