1. Home
  2. Computing & Technology
  3. Delphi Programming

How to dynamically change the Page title in ASP.NET

By , About.com Guide

To allow the asp.net (aspx) web page title (caption) to change for different pages, you can use HtmlGenericControl. Just set an ID for the page's TITLE tag and add a runat="server" attribute. Once you make it server-side element, you can access the control from the code and set any value for it.

For the start make sure your aspx page header looks like:

<HEAD>
<TITLE ID=PageCaption RUNAT="server"></TITLE>
</HEAD>
...

Now, inside the page unit's code declare a variable of type HtmlGenericControl and set the InnerText property of that variable in you code behind file. You can use any of the page events, the example below uses Page Load event.

~~~~~~~~~~~~~~~~~~~~~~~~~
type
   TWebForm1 = class(System.Web.UI.Page)
...
   strict protected
     PageCaption : System.Web.UI.HtmlControls.HtmlGenericControl;
...

procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs) ;
begin
   // TODO: Put user code to initialize the page here
   PageCaption.innerText:= 'My dyna caption - server at:' + DateTime.Now.ToString;
end;

~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to move PageControl's tabs using drag'n'drop
« How to set the "home page" for the Internet Explorer from Delphi code

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. How to dynamically change the Page title in ASP.NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.