Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 860
bh213 Avatar asked Nov 28 '08 20:11

bh213


People also ask

How do I apply master layout to a view?

Creating a View Master Page You add a new view master page to an MVC project by right-clicking the Views\Shared folder, selecting the menu option Add, New Item, and selecting the MVC View Master Page template (see Figure 1). You can create more than one view master page in an application.

What is master page in ASP NET MVC?

In asp.net we all are familiar with Master page. Master page is used to create a common layout for the web based application. In Master page we use Content Place Holder where we want to place other pages content. Similarly we use the concept of Master page in MVC. We create a View which will be common to every page.

How do I add a View Master Page in MVC?

You add a new view master page to an MVC project by right-clicking the Views\Shared folder, selecting the menu option Add, New Item, and selecting the MVC View Master Page template (see Figure 1). Figure 01: Adding a view master page ( Click to view full-size image)

How do I set the page title in ASP NET?

An ASP.NET page can specify its title in one of the following ways: 1 By placing the value directly within the <title> element 2 Using the Title attribute in the <%@ Page %> directive 3 Programmatically setting the page's Title property using code like Page.Title="title" or Page.Header.Title="title". More ...

What is layout page in ASP NET MVC?

Layout is similar to the master page in ASP.NET Web Form. Similar to master page, the Layouts may contain CSS, jQuery files and multiple views. Layout can keep the user interface elements and theme consistency throughout the application. Layouts are the special view type in ASP.NET MVC. Hello Nitin Pandit…………This is the demo of Layout pages.

What is the default title of a page in Visual Studio?

By default, Visual Studio sets the <title> element in the master page to "Untitled Page". Similarly, new ASP.NET pages have their <title> set to "Untitled Page", too. Because it can be easy to forget to set the page's title to an appropriate value, there are many pages on the Internet with the title "Untitled Page".


5 Answers

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.

like image 77
Andrew Csontos Avatar answered Sep 25 '22 01:09

Andrew Csontos


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.

like image 29
hugoware Avatar answered Sep 25 '22 01:09

hugoware


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"].

like image 33
dtc Avatar answered Sep 23 '22 01:09

dtc


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)

like image 23
reddi.eth Avatar answered Sep 24 '22 01:09

reddi.eth


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".

like image 22
Venugopal M Avatar answered Sep 23 '22 01:09

Venugopal M