How to change page's title dynamically


G

Guogang

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
 
Ad

Advertisements

L

Laidbak

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.
 
G

Guogang

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.
 
S

Showjumper

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
 
G

gerry

this works , but VS has the annoying habit of removing the runat="server"
from <TITLE> tags when it feels the urge.
 
M

Michael Earls

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
 
Ad

Advertisements

G

gerry

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.
 
Ad

Advertisements

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

Similar Threads

HTML Anchor tag not working 2
Using CSS ID Tags to 2
Horizontal menu bar header 2
Passing flask textbox value to an infinite while loop 0
Top NOT top? 2
External html 2
Why <link/> is not working? 4
Extends 2

Top