How to change page's title dynamically

Guogang

Usenet Archive
Joined
Apr 9, 2003
Messages
32
Reaction score
0
Hi, is there a way to change the page's title in code behind (C#)?

I want to change this part:
<HEAD>
<TITLE> Can be changed dynamically. </TITLE>
</HEAD>

Thanks,
Guogang
 

Laidbak

Usenet Archive
Joined
Jan 20, 2004
Messages
13
Reaction score
0
Hi, is there a way to change the page's title in code behind (C#)?

Make the text of the title a label.
In your code behind set the label's text.
 

Guogang

Usenet Archive
Joined
Apr 9, 2003
Messages
32
Reaction score
0
Well, it works. Never thought I can do this. :)

Only thing I want to change is that I need to use "Literal" instead of "Label", otherwise, the title will have <span> around it.

Laidbak said:
Hi, is there a way to change the page's title in code behind (C#)?

Make the text of the title a label.
In your code behind set the label's text.
 

Showjumper

Usenet Archive
Joined
Jun 26, 2003
Messages
242
Reaction score
0
Give your title tag an id and runat attribute. then declare it in code behind And then you can retrive data from a dataset and set the inner text property equal to the text or whatever.

<title id="PageTitle" runat="server"></title>

Protected WithEvents PageTitle As HtmlGenericControl

PageTitle.InnerText = ds.Tables(0).Rows(0).Item("ArticleTitle").ToString
 

gerry (archive)

Usenet Archive
Joined
Jul 17, 2001
Messages
351
Reaction score
0
this works , but VS has the annoying habit of removing the runat="server" from <TITLE> tags when it feels the urge.
 

Michael Earls

Usenet Archive
Joined
Jan 23, 2004
Messages
24
Reaction score
0
Gerry,

The problem with what you are trying to do is that ASP.NET treats everything from the first character of the output (usually "<!DOCTYP..." for VS.NET created WebForms) to the first control as a LiteralControl. this means that the title is within this LiteralControl as literal text to be spit out (without further processing) at Render-time. This is very efficient, but puts the title out of reach for the base implementation of System.Web.UI.Page.

The only sure-fire way to completely control this is by creating a class that derives from System.Web.UI.Page and then adding private variable and an accessor for a property named "Title".

Then, override the Render method. In that method, you'll need to find the <title> control in the text of that first literal control and change what's between <title> adn </title> to your internal title variable.

You can view the code (and description) here:

http://www.cerkit.com/cerkitBlog/PermaLink.aspx?guid=54fe3c55-91ca-4318-856d-c1e62963db9b
 

gerry (archive)

Usenet Archive
Joined
Jul 17, 2001
Messages
351
Reaction score
0
Thanks for the info michael, I will incorporate this in my base page.

However, imho, this is still a bug in VS in that it is 'randomly' removing things from my html. The runat="server" method works just fine until VS removes it - I haven't pinned down exactly when it is removed.
 
Joined
Sep 6, 2013
Messages
1
Reaction score
0
Very Simple

Copy And Paste it to page_load


var page = (Page)HttpContext.Current.Handler;
page.Title = "someTitle";

:lol:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top