ASP.NET Master Page & Dynamic Page Title

C

Chris Walls

We have a web application that is using a master page. This application is
being localized for multiple languages so for that, and other reasons, we
would like set the page title in my code behind. We've tried using the
following syntax. It doesn't raise an exception, but the title is not being
updated to the desired text.

this.Master.Page.Title = "My Page Title";

How can we programtically set the page title from a content page? Any help
would be appreciated.

- Chris
 
D

David Wier

I'm curious - normally the master page doesn't set the title, since each
content page has it's own, that you can set either in the property window,
or by code-behind:
me.title="whatever"
Are you setting the page title for every content page, in the master page?

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://iWritePro.com
 
M

Mark Rae [MVP]

I'm curious - normally the master page doesn't set the title, since each
content page has its own, that you can set either in the property window,
or by code-behind:
me.title="whatever"

That's right.
Are you setting the page title for every content page, in the master page?

I'm not - the OP may be, though...
 
C

Cowboy \(Gregory A. Beamer\)

The Master Page title is only used if there is nothing else to grab onto.
While it appears as a container, it is actually a control in the ASPX page.
You want to set the title of the Header, as Mark has stated.

For globalization, you can also use the title="" in the @Page directive and
set it to a resource string. If you are using custom resource providers, you
may have to do a bit of magic here in the Page_Init routine, but if you are
using standard resource files, it is the same as any other resource tag
pulling from the file rather than meta.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
 
C

Chris Walls

Didn't work. No exception thrown but the HTML title tag does not contain
the value I'm setting Header.Title to.

To answer other questions, I'm not setting the title from the master page,
I'm trying to set it programatically from the content page, though
unsuccessfully.
 
M

Mark Rae [MVP]

Didn't work. No exception thrown but the HTML title tag does not contain
the value I'm setting Header.Title to.

Where are you using the Header.Title = "...."; code? Can you put a
breakpoint on that line and inspect the value of Header.Title before and
after it runs...?
 
W

Walter Wang [MSFT]

Hi Chris,

Please try following test pages:

MasterPage.master:


<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Master Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>


Default2.aspx:

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

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
this.Header.Title = "Test";
}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:Content>




The Default2.aspx should correctly show "Test" as the web page title.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chris Walls

Got it now. We had a default title set in the master page's HTML. Once it
was removed, the code worked. It was thought this would be overwritten by
the content page's page_load method.

Thanks for the help.
 

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

Top