If i have an ASP.NET MVC 2 Web Application with the following Views:
Main.aspx
Page1.aspx
Page2.aspx
And all Views inherit from a MasterView called Site.master,
I want to be able to have a default Title/H1 for the page, which can be overriden in the deriving Views.
E.g Main.aspx would have "MySite - xxx", Page1.aspx would have "MySite - Page 1", Page2.aspx would have "MySite - Page2".
And if i choose not to set the Title/H1 in a new derived view, the master Title/H1 would be displayed.
With WebForms, i would accomplish this in the following way:
Make the Title/H1 tags on the master runat="server"
Expose protected properties on the master code-behind
On Page_Load of the derived pages code-behind, set the property (Master.Title = "Page 1").
On Page_PreRender of the master code-behind, set the Title/H1 tags to "My Site - " + Title;
How can we accomplish this with ASP.NET MVC?
I could do this in the master:
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
And then set it in the view:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MySite - Page 1
</asp:Content>
But i wanted to be able to only specify "Page 1" in the View, and the title gets magically changed to "MySite - Page 1". Know what i mean? The "MySite - " part of the title needs to be the template for the title.
I'm probably missing something obvious here. :)
With a quick search I found this:
http://haacked.com/archive/2009/04/03/tipjar-title-tags-and-master-pages.aspx
explains why
<title>MySite<asp:content..../></title>
doesn't work
This is how I usually do it:
<title>MySite - <%: Page.Title ?? "Default title" %></title>
in your MasterPage.
Then, you can define the Title property on a content page like this:
<%# Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage"
Title="Page 1"
%>
Edit:
Well, you might want to see this SO question: ASP.NET MVC - View with master page, how to set title?.
Is much easier as you are describing it.
Just add a content placeholder to your master page
<title>
My Site - <asp:ContentPlaceHolder ID="PageTitle" runat="server" />
</title>
Then in your Content Page use it as
<asp:Content ID="Content3" ContentPlaceHolderID="PageTitle" runat="server">
My Page
</asp:Content>
This way will work but you have to use an HTML HEAD tag, not a server control. So simply remove runat="server" from your HEAD.
Related
Ok this an odd way of doing this, I know I should have done it on Page_Load in every pages when using Masterpage, but is there a way around this issue? I don't want to rebuild the pages if I can help it,if I can only insert a title on <asp:content></asp:content> it would be easier.
If anyone has run into this or maybe have a suggestion of a good way to do it , I know jquery can do it document.title ='' but I heard it's not SEO friendly.
Thanks!
You can still set the title in each page that's using a MasterPage. In markup: -
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
Or in code: -
protected override void OnLoad(EventArgs e)
{
Page.Title = "Your Title";
base.OnLoad(e);
}
you can place a contentplaceholer in your master page as..
<asp:ContentPlaceHolder id="head" runat="server"> </asp:ContentPlaceHolder>
After that In content page add its reference as..
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title> Your title goes here. </title>
</asp:Content>
more simple solution in Master page
<%: Page.Title %> - the main title goes here
in content page first line of it
<%# Page Title="Your Title" Language="C#" MasterPageFile="~/_masterpages/... etc
I know this is an older question but what definitely works for me is to set a Literal control in the title tag of the MasterPage like this:
<title><asp:Literal runat="server" id="pagetitle" Text="MyTitle"></asp:Literal></title>
Then, in the content page, put this in the Page_Load method:
Literal myTitleLiteral = (Literal)Master.FindControl("pagetitle");
if (myTitleLiteral != null)
{
myTitleLiteral.Text = "Test Title";
}
My ASP.NET page was not showing the controls. It just shows that master page error
The page has one or more <asp:content> controls that do not correspond
with <asp:ContentPlaceHolder controls in the Master Page
Try setting
<title></title>
instead of
<title />
Remove comment (<%-- --%>) in the Master file, this worked for me.
All your Content controls have to reference existing ContentPlaceHolders on master page
For instance, if you have on your page
<asp:Content ContentPlaceHolderID="Header" ID="Title" runat="server">
You must have something like this on your master page
<asp:ContentPlaceHolder ID="Header" runat="server" />
ContentPlaceHolderID is the property that must match against any ContentPlaceHolder ID on master page.
The problem is that you must have a syntax error in the master.page's markup :
Ex) %²> in stead of %>
If you validate your markup, I'm sure thar this will solve your problem
PI. I has the same error & it works for me ;)
To manage the page title on my pages, I have a master page with a ContentPlaceHolder in the header.
<head runat="server">
<asp:ContentPlaceHolder runat="server" ID="headContent">
</asp:ContentPlaceHolder>
</head>
On each of my pages, I add the meta tags and page title as follow:
<asp:content id="Header" contentplaceholderid="headContent" runat="server">
<meta name="keywords" content="keyword1, keyword2" />
<meta name="description" content="page description" />
<%Page.Title = "My page title";%>
</asp:content>
I cannot modify the code on the pages by placing the Page.Title in the OnInit method of the page.
I need access to the page title in the codebehind of the master page, but I always get an empty title when I use Page.Title.
By using <%Page.Title = "My page title";%> you implicitly tell the ASP.NET to execute this embedded code block during the page's render phase.
What does it mean? You won't be able to get this value before the page's render phase. Assuming you're trying to get this value a little bit earlier than during the render. That's why you get the empty string.
The workaround could be in setting your Title property of the <%# Page directive in the beginning of your page e.g.:
<%# Page Title="My Title Goes Here" Language="C#" ... %>
By setting this you'll be able to access the Page.Title property from your master page a little bit earlier than the page's rendering occurs.
Just use
<title>My page title</title>
I want to change Page title from partial view. But following error shown
Using the Title property of Page requires a header control on the page. (e.g. <head runat="server" />).
My master page head section is here
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
My Default page is here
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%=ViewData["pagetitle"] %>
</asp:Content>
ViewData["pagetitle"] returns current page title for example Home, About, News List, News Detail. But i want to change current news information title instead of News Detail string. News Detail page contains partial view. Partial view know which news shown.
Please help
Not really sure how you have your files structured but if you have an entities folder in which you pull information from for your view page then in the aspx page you can do something like this:
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:projectName.Resources.Entities.FolderName.FileName%>
</asp:Content>
Because the title is rendered as part of the head section, there is no easy way for a partial view to set the page title directly.
But your controller (which fetches the news item to display) will know what the title should be, and can add the "pagetitle" item to the ViewData collection, which you can then set either in your master layout directly or in a page-specific content region as you are doing above.
Hint: I'd recommend creating a static class called ViewDataKeys with static string properties (or public const fields) used as indexers into the ViewData collection. This helps avoid duplicates, spelling mistakes and case-sensitivity errors in working with the string-based dictionary from multiple sources.
I have a Master Page for all my ASP.NET pages. It works fine for 95% of pages... Except that it also includes unwanted attributes that are included in the other 5% of pages. This causes application problems. Is it possible for Content Pages to somehow select to enable/disable a feature available in the master page? One option I can think of is for the Master Page to look at the page name and then decide what to do, but that's a bit clunky in the long run...
You can add a MasterType directive in your content page so you will have access to the master page class... and from there you can implement to enable disable features of your master page...
<%# MasterType virtualpath="~/MyMaster.master"%>
and in your code behind you will have access to the Master property as a typed class...
In your master page provide a content placeholder:
<asp:ContentPlaceHolder ID="foo" runat="server">
<div>Some default content</div>
</asp:ContentPlaceHolder>
In the 5% of the pages that don't need the default behavior override this placeholder with an empty string:
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="foo" />
The other 95% of the pages will get the common behavior.
You can access the master page directly by getting at the page and casting it to the type of your master page as below. This will couple you to the master page but the only other option I can think of is to create a key in session.
(MyMasterPage)Page.Master
Assuming that these features are represented in the asp markup of the master page, you could wrap it within a ContentPlaceHolderControl:
<asp:ContentPlaceHolder ID="OptionalContent" runat="server">
*** This is my optional content ***
</asp:ContentPlaceHolder>
In the 95% of pages where this content works, you can just not include a Content control for "OptionalContent" on your page. For the 5% where it doesn't work, you can include an empty Content control for "OptionalContent":
<asp:Content ContentPlaceHolderID="OptionalContent" runat="server"></asp:Content>