Just curious, it's easy to change the page title when each view is it's own full HTML page, just put a <%=ViewData.PageTitle %> in where the <title> tag is (assuming you pass a PageTitle property to the view). But what about when the view is only a content
page of master page? (sorry, the question came to mind while I'm at a location I can't test, does the title property in the page declaration still work?)
The results from that search aren't speaking to the MVC framework. So I'm assuming I'd need to make the <head> tag a server control and set the page title the way suresh_g_v2002 mentioned.
I thought the MVC framework was trying not do things that way. I guess there probably isn't a way to do it without setting the <head> tag to runat="server".
The point i was making is that this is not an mvc question. it's a problem common to all asp.net apps.
The way I've solved this is to ignore the default asp.net Title handling. I've created an interface with a property for the title. created a method on the master page to cast Page to the interface type allowing my to effectively have the following...
no runat="server" but it means that EVERY page which uses that master page needs to implement the interface. for me that's not such a big deal but i know others will disagree.
Marked as answer by ChadThiele on Jan 02, 2008 05:18 AM
The point i was making is that this is not an mvc question. it's a problem common to all asp.net apps.
The way I've solved this is to ignore the default asp.net Title handling. I've created an interface with a property for the title. created a method on the master page to cast Page to the interface type allowing my to effectively have the following...
no runat="server" but it means that EVERY page which uses that master page needs to implement the interface. for me that's not such a big deal but i know others will disagree.
That's what I was looking for... but, the problem doesn't exist in a typical web forms page because you simply access the Page.Title property in your code behind (even if the content page is using a master page). Anyway, you answered my question, thank you.
Sorry to drudge this back up... but I just wanted to let everyone know how I'm handling this. This won't work for everyone, but it works perfect for this particular app.
In this case, I want to have the page title be the same as the first <h1> in the page...so like Suresh pointed out, I can set the page title using Javascript. So I just use jQuery to do something like this:
window.document.title = $("firstH1").text();
In my app, the first h1 is basically a big breadcrumb data navigation trail, having the page title the same make it more SEO friendly and improves SERP readability (people know what they are going to before clicking the link). The text inside the first h1
is rendered by the view using various bits of ViewData...
ChadThiele
Participant
983 Points
274 Posts
Page Titles When Using Master Pages
Jan 02, 2008 01:20 AM|LINK
Just curious, it's easy to change the page title when each view is it's own full HTML page, just put a <%=ViewData.PageTitle %> in where the <title> tag is (assuming you pass a PageTitle property to the view). But what about when the view is only a content page of master page? (sorry, the question came to mind while I'm at a location I can't test, does the title property in the page declaration still work?)
prowla2003
Member
546 Points
138 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 01:39 AM|LINK
<%@ Page Language="C#" MasterPageFile="~/Site1.Master"
AutoEventWireup="true"
CodeBehind="MyPage.aspx.cs"
Inherits="MyCompany.MyProject.WebUI.MyPage"
Title="MyPage Page Title" %>
If your interested in Reflexology in Sydney then click on the link...
ChadThiele
Participant
983 Points
274 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 03:08 AM|LINK
I'm sorry, I didn't clarify that I meant programmatically set the page title.
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Page Titles When Using Master Pages
Jan 02, 2008 03:25 AM|LINK
http://www.google.co.uk/search?hl=en&q=programmatically+set+page+title+asp.net
suresh_g_v20...
Contributor
3566 Points
824 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 03:34 AM|LINK
Hi ChadThiele ,
you can do it using javascript
Eg: window.document.title = "Title of the Page";
Suresh Kumar G
suresh_g_v20...
Contributor
3566 Points
824 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 03:45 AM|LINK
Hi ChadThiele ,
in the code behind file you can do it in the following way
HtmlTitle htTitle = new HtmlTitle();
htTitle.Text = "Title of the page";
Header.Controls.Add(htTitle);
Hope this helps you
Suresh Kumar G
Mark as answer if you get the solution
ChadThiele
Participant
983 Points
274 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 04:39 AM|LINK
The results from that search aren't speaking to the MVC framework. So I'm assuming I'd need to make the <head> tag a server control and set the page title the way suresh_g_v2002 mentioned.
I thought the MVC framework was trying not do things that way. I guess there probably isn't a way to do it without setting the <head> tag to runat="server".
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Page Titles When Using Master Pages
Jan 02, 2008 05:00 AM|LINK
The point i was making is that this is not an mvc question. it's a problem common to all asp.net apps.
The way I've solved this is to ignore the default asp.net Title handling. I've created an interface with a property for the title. created a method on the master page to cast Page to the interface type allowing my to effectively have the following...
<head>
<title><%= MyPage.MyTitle %></title>
</head>
no runat="server" but it means that EVERY page which uses that master page needs to implement the interface. for me that's not such a big deal but i know others will disagree.
ChadThiele
Participant
983 Points
274 Posts
Re: Page Titles When Using Master Pages
Jan 02, 2008 05:20 AM|LINK
That's what I was looking for... but, the problem doesn't exist in a typical web forms page because you simply access the Page.Title property in your code behind (even if the content page is using a master page). Anyway, you answered my question, thank you.
ChadThiele
Participant
983 Points
274 Posts
Re: Page Titles When Using Master Pages
Jan 04, 2008 01:38 AM|LINK
Sorry to drudge this back up... but I just wanted to let everyone know how I'm handling this. This won't work for everyone, but it works perfect for this particular app.
In this case, I want to have the page title be the same as the first <h1> in the page...so like Suresh pointed out, I can set the page title using Javascript. So I just use jQuery to do something like this:
window.document.title = $("firstH1").text();In my app, the first h1 is basically a big breadcrumb data navigation trail, having the page title the same make it more SEO friendly and improves SERP readability (people know what they are going to before clicking the link). The text inside the first h1 is rendered by the view using various bits of ViewData...
Works great!