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