Programming
Article
By Philip Miseldine

Setting Page Titles 2005 Style

By Philip Miseldine

Little tip.

To set the page title on an ASP.NET Web From using Whidbey, add runat=”server” properties to the head and title tags in your HTML:





Now, you can set the page easily using:


this.Title = "My new page title";

Simple as!

  • Cory M.

    Ooh, very nice.

  • andre

    uh, how do you do this under ASP.NET 1.1? using client-side javascript?

  • Just outta curiousity, are they putting that class in the HtmlControls or WebControls namespace?

  • Andre: You need to set the page title tag as with .NET 2.0 (i.e. with a runat=”server”) but define the following in your codebehind:


    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

    Spiff Dog: Its a string property on the Page class…

    “this.” is just an easy way of referencing the current instance :) So, its the System.Web.UI.Page class technically :P

  • Creating all of that code just for a title, of course, really doesn’t buy you much. You should really consider placing page titles, descriptions and keywords in the web.config file user defined section such as:





    and then reference this using the script name from the web pages. This allows easy page editing in a single file.

Recommended
Sponsors
Get the latest in Front-end, once a week, for free.