Inserting Page Title in HTML Automatically

J

Jonathan Wood

I'd like to have a page's title appear within the page (ideally, by
inserting something in a master page).

I tried the following:

xxx <%# Page.Title %> xxx

I tried this in both a content page and a master page. It compiles without
error but, in both cases, the two "xxx" showed up with only a space between.

Is this possible? I would really prefer not to use a label that is set from
code.

Thanks.
 
J

Jonathan Wood

Playing around with this, I came up with:

<h1><% Response.Write(Page.Title); %></h1>

Which seems to work.

Can anyone tell me if this is the most efficient way? In particular, I was
hoping for something the compiler could actually replace the text with the
name of the page, so the code wouldn't be necessary at runtime. But my hunch
is that this would involve that bit of code running everytime the page
loads.

Thanks.
 
J

Jonathan Wood

Peter,
I'm not sure I completely understand what your goal is here, but the Page
class in ASP.NET 2.0 has a handy "Title" property, e.g.

Page_Load( xxxx )
{
Page.Title ="This is the Page Title Dood!";
}

I'm not trying to set the title text. I'm trying to insert the title text
within my HTML text.

I could create a label and set the label's text from the Load handler. But
that means that I'll need this little bit of code in every single one of
dozens and dozens of pages. That's why I said I would really prefer not to
use a label that is set from code.
 
J

Jonathan Wood

Mark,
xxx <%= Page.Title %> xxx

# is databinding syntax...

Makes sense. I was trying to duplicate something I'd seen in a gridview
template. I had a hard time finding a good reference on the different
syntaxes and how they are used. (Google won't search on punctuation and the
few books in front of me do not list <% in the index.)

Thanks.
 
B

bruce barker

the easiest and cleanest is to create your own control. just a couple of
lines of code:

public class PageTitle : Label
{
protected override void Render(HtmlTextWriter output)
{
Text = Page.Title;
base.Render(output);
}
}


then you can

<myControls:pageTitle runat="Server">

anywhere you want the title to render.

-- bruce (sqlwork.com)
 
J

Jonathan Wood

I'm not sure if I think that is a better solution, but it is an interesting
approach.

Thanks!
 

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
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top