RSS

Changing Page Title on the fly ASP.NET

19 Nov

If I asked you to


change the title of your Web form

dynamically

, where would you look ??

you’d be forgiven for looking for a “title” or “text” property somewhere. The problem is, that mysterious property doesn’t exist no prizes for guessing y!!.

Here’s howto.

Step 1 >> switch to the HTML view on your Web form. Near the top of your page, you’ll see the title tag, looking something like :

” WebForm1 ” .

Replace this with ”’.


The quotes are just added to get rid of the parser error i had

becoz of having a original Title tag in the post and plus one more in my code

sample , the code is without the quotes

Here, you’re creating a title tag that runs on the server and has an ID, meaning you can manipulate it in code.

ie add a runat server attribute and an ID to the tag

Step 2 >> switch back to design mode; then, open up the code window behind your form. This declares the server title tag you’ve just added:

protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;

so that its accessible in ur code behind.

Step 3 >> changing the page text. Behind the Load event of your page, or in response to some similar event, set the InnerText property of the PageTitle tag to your new page title. Here’s my sample code:

PageTitle.InnerText = “Welcome! – Last Updated 20/05/2003”;

And that’s it Presto —one line of code and your page title has changed.

P.S One thing to note is that :- the moment you make any changes in the UI of the ASPX Page Visual studio gets rid of the

Advertisements
 
Leave a comment

Posted by on November 19, 2004 in .NET

 

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: