RSS

Changing Page Title on the fly ASP.NET


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 :


'<' title '>' WebForm1 '<'/title'>' .

Replace this with '<'title runat="server" id="PageTitle"'>'<'/title'>'.



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 < runat = "server">









0 comments

Posted in

Post a Comment

Copyright © Shounak S. Pandit. Powered by Blogger.

Ads