ASP.NET MVC - View with master page, how to set title?
Asked Answered
P

11

16

What is prefered way of setting html title (in head) for view when using master pages?

One way is by using Page.Title in .aspx file, but that requires in master page which can mess with HTML code. So, lets assume no server side controls, only pure html. Any better ideas?

UPDATE: I would like to set title in view NOT in the controller or model.

Prink answered 28/11, 2008 at 20:40 Comment(0)
P
-4

We ended up with

<head runat=server visible=false>

in master page.

This way we can read from Page.Title (Page.Title requires head element to exist, otherwise it throws an exception, checked that with reflector). We then use our own head element - MVC way.

Prink answered 2/12, 2008 at 14:44 Comment(0)
L
32

In our master pages, we created both an "init" ContentPlaceHolder, and a "title" ContentPlaceHolder. If someone wants to programatically set Page.Title, they can set it in CSharp in the init placeholder, or they can override the "title" placeholder using tags.

Master Page

<asp:ContentPlaceHolder id="init" runat="server"></asp:ContentPlaceHolder>
<head runat="server">    
    <asp:ContentPlaceHolder ID="title" runat="server">
        <title><%=this.Page.Title%></title>
    </asp:ContentPlaceHolder>
</head>

View Page Could either override the entire "title" content placeholder:

<asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server">
       <title>Home Page</title>
</asp:Content>

or programatically set the page title.

<asp:Content ID="Content1" ContentPlaceHolderID="init" runat="server">
    <%this.Title = "Home Page";%>
</asp:Content>

Make sure you remove the Title="" from the Page directive at the top, or you won't be able to programatically change Page.Title.

Loram answered 17/3, 2009 at 6:7 Comment(2)
You only need the runat="server" in the head tag if you intend to populate the title from the Title="" directive at the top of your view. Otherwise it's better to leave it out.Guillory
This was perfect - I needed a way to support this in legacy Web Forms-based views. Of course it's already supported via Razor with the ViewBag.propertyName syntax that can lead all Razor views before declaring their layout page. Thanks again!Massie
E
11

I see a lot of people that use the <%= ViewData["Title"] %> option.

I suppose you could also insert a ContentPlaceHolder named Title and then just use that on your page, but in all the MVC examples I've seen, they use the first option.

Elouise answered 28/11, 2008 at 23:52 Comment(0)
B
2

When I create a new MVC project it has files in there and uses a master page. Looking at that it seems it passes the title to the ViewData as ViewData["Title"] and in the master page, in the <head> there is a script block that outputs ViewData["Title"].

Bea answered 28/11, 2008 at 22:49 Comment(0)
C
1

I ended up using a code-behind file to implement Page.Title="..." in the Page_Load() method.

I didn't like doing this, however attempts to implement the change directly in the .aspx page did not work, as it resulted in two <title> tags being present, the one I generated, and the one generated by the Master file the page inherited from.

Ideally, my page code should have overridden the master file's <title> value, but I guess this is just one of those quirks that ASP.Net MVC still has, and one that may already be fixed in a newer version of the ASP.Net MVC Framework (we're still on ASP.Net MVC Beta)

Conurbation answered 10/3, 2009 at 13:9 Comment(0)
E
1

You could do this in your Master Page:

<title>
    <%:MyTitle + " :: " %>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server">
    </asp:ContentPlaceHolder>
</title>

where MyTitle = some value from your web.config or hard text like "My Web"

Now in the view pages (Index for example):

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%:"My Home Page"%>

Now when you browse your home page, the title would be "My Web :: My Home Page".

Evictee answered 14/8, 2014 at 10:24 Comment(1)
Hi, I like this approach! It works! And I make some modification on it to put the " :: " separator from the master page to the view page(s), thus we can avoid showing an empty " :: " on the home page.Snowfall
A
0

I have a base view class that sets the page title from a resource file. Works pretty good for me.

Anthropogeography answered 15/1, 2010 at 7:18 Comment(0)
P
0

I created a class called Application with a Title property (using get/set):

public static class Application
{
    static string title;

    public static string Title
    {
        get { return title; }
        set { title = value; }
    }
}

I then set the property on the Page_Load event for each page:

Application.Title = "Silly Application";

then i just reference that property on the master page:

<div id="divApplicationTitle">
     <asp:HyperLink runat="server" NavigateUrl="~/Default.aspx"><asp:Image ID="imgApplicationImage" runat="server" SkinID="skinApplicationLogo" /><%=Application.Title%></asp:HyperLink> 
</div> 
Plenteous answered 13/5, 2015 at 16:38 Comment(0)
H
-1

There is a Title property of the @Page directive for content pages.

Hypothermia answered 28/11, 2008 at 20:44 Comment(6)
Yes, but I said above, it requires <head runat=server> in master page, which is not what I want.Prink
Your question is nonsensical then, because you're saying you want to set the title of your content page using server-side constructs, but you don't want to use server-side constructs.Hypothermia
Well, I would like to avoid any aspx controls. <head runat=server> eats some of the html we use, which we could solve, but I would prefer to use only pure html. Master pages and controls should be acceptable though.Prink
If you're using MVC... you really shouldn't be relying on "runat=server" stuff anyway. Otherwise, why are you using MVC :)Prudence
Because MVC is a design pattern that has little to do with the whole "purist" write-it-all-in-the-aspx-page argument.Hypothermia
BTW... "Yes, but I said above, it requires <head runat=server> in master page, which is not what I want." You did not say that in your post, you should re-read what you posted and fix your post if that's what you meant to say. Proof-reading FTW.Hypothermia
R
-1

For ASP.NET content pages just add Title="" in the <%@ %> Placeholder.

Responser answered 3/12, 2008 at 0:11 Comment(0)
P
-4

We ended up with

<head runat=server visible=false>

in master page.

This way we can read from Page.Title (Page.Title requires head element to exist, otherwise it throws an exception, checked that with reflector). We then use our own head element - MVC way.

Prink answered 2/12, 2008 at 14:44 Comment(0)
E
-8

You could always use javascript in your view page:

<script type="text/javascript>
    document.title = "Hello World";
</script>
Exiguous answered 30/11, 2008 at 17:19 Comment(1)
Don't do this since JavaScript is never a 100% given.Sprung

© 2022 - 2024 — McMap. All rights reserved.