tags:

views:

158

answers:

3

I'm new to ASP.NET and want to have an asp:content control for the page title, but I want that value to be used for the tag and for a page header. When I tried to do this with two tags with the same id, it complained that I couldn't have two tags with the same id. Is there a way to achieve this with contentplaceholders, and if not what would be the easiest way to use a single parameter to the masterpage twice in one page?

+1  A: 

Title is actually an attribute on content pages, so you do something like:

<%@ Page Language="C#" MasterPageFile="~/default.master" Title="My Content Title" %>

on the content page. To get that into a header, on the master page just render the page title:

<h1><%= this.Page.Title %></h3>
Mark Brackett
Is there a way to generalize this if I need to do something similar for another attribute?
Not sure what you mean...maybe you could edit to include a code sample of what you'd *like* to be able to do?
Mark Brackett
I think I figured out what I was not very clearly trying to ask.I was having trouble getting this.Title to work, I think it's supposed to be this.Page.Title
Oy - you're absolutely right. In a master page, you should reference this.Page. I'll edit the answer.
Mark Brackett
A: 

Hi,

I see that someone has just provided a (far) better answer to this specific problem. You could use the solution below if you have a master page that has the same content in multiple places (excluding the title).

The best solution I can come up with is the following:

Add an asp:Label for the page title and another one for the second position you want the text to appear (use two different id's, for example: pageTitle and sameTitle)

Add a method to your master page:

public void SetPageTitle(string title)
{
    pageTitle.Text = title;
    sameTitle.Text = title;
}

On your content page, call the master page method. If your content page has a master page, it has a property called Master. You can now call the SetPageTitle method from your content page PageLoad method:

((MyMasterPage) Master).SetPageTitle("My content page");

You can also use the MasterType directive in your master page, check here for more info. This way you get a strongly-typed Master property that you do not have to cast:

Master.SetPageTitle("My content page");

Regards,
Ronald

Ronald Wildenberg
A: 

Also, master pages can have different content tags (with different Id's) which is good if you want pages to potentially alter various parts of the page.

flukus