views:

551

answers:

5

I need to set the title of a page dynamically, and so I use code similar to the following:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="~/about.aspx.cs" Inherits="Default" %>
<%@ Register Assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<%@ MasterType VirtualPath="MasterPage.master" %>
<%@ OutputCache Duration="43200" VaryByParam="*" Location="Server" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 <title><%=pageTitle%></title>
</asp:Content>

But this generates duplicate title tags. Is there any way I can get around this? Thanks.

EDIT: Following from the suggestions below, I now have the following in my MasterPage:

<head id="Head1" runat="server">
<title>Default Title</title>
...
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head> 

and the following in my primary page:

    this.Title="xxx";

but I'm not getting any title (neither "Default Title" nor "xxx").

EDIT: Nevermind. Got it working using that method.

A: 

Try this instead

        ((MyPageClass)Page).Title = "Your Page Title"

This code goes in the Page_Load of your Master page. I have to use a cast ("MyPageClass") because I derive my own PageClass so I can put strongly typed session objects there. If you don't, then you won't need the cast.

If you need to do this at the page level, I think you can just use:

     Title = "Your Page Title"

in your Page_Load.

Mark Brittingham
Where should this go? I tried putting it in Page_Load and that doesn't work. Thanks.EDIT: Just saw your edit. In the main page, I now have Master.pageTitle="xxx"; and in the MasterPage, I have Page.Title=pageTitle, but that doesn't work.
alpheus
It is the Page_Load of the MASTER page. If you need a different title for each page, then you'll have to call into a function defined in the Master page object. There is a separate trick to this but its just bookkeeping so let me know if you need help with it.
Mark Brittingham
I think I misunderstood your problem a bit. I thought that you wanted to set it at the Master page level - but dynamically. It is a trivial problem if setting it at the Page level but you don't do it in HTML because the Master page should have the only <head> tags. Instead, you do it in the Page_Load.
Mark Brittingham
+3  A: 

The header of your .master needs to look like this:

<head runat="server">
<title>Default Title</title>
  .. other tags
</head>

Then in your page code-behind in the Page_Load, you write:

protected void Page_Load(object sender, EventArgs e)
{
    this.Title = "My Page Title";
}
Keltex
A: 

If in your master page the title section looks like this:
<title><%=someTitleVariable%></title>

then use this as your code instead:
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> <%=pageTitle%> </asp:Content>

Kaius
A: 

In defining a <asp:Content ContentPlaceHolderID="head"> control you're not modifying the title which is already there, but instead you're adding more markup to the Content Place Holder with ID "head". In your Master Page, I'd imagine you have something like this:

<head>
  <title>Master Page Title</title>
  <asp:ContentPlaceHolder id="head" runat="Server" />
</head>

So the <asp:ContentPlaceHolder> gets replaced with the <asp:Content ContentPlaceHolderID="head"> so you end up with a second <title> element.

So either remove the <title> from your Master page - which means you'll need the <title> in all your aspx files - or use some code to do something like this...

this.Title = pageTitle;
// if that doesn't do it try
// this.Header.Title = pageTitle;
LeguRi
Interestingly, I did not have <title> in my MasterPage, but it was generating that tag twice nevertheless.
alpheus
A: 

To avoid the duplicate tags just do the following, no extra code.

In your MasterPage have something like this:

<head>
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>

And on every other page add the Title="Your Title" attribute to the page directive:

<%@ Page Title="Your Title" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

This eliminates your duplicate tags and puts the title in a clear, visible place at the top in code view.

Hope this helps.

Manuel