Skip to Main Content
ASP.NET 4 24-Hour Trainer
book

ASP.NET 4 24-Hour Trainer

by Toi B. Wright
July 2010
Beginner content levelBeginner
552 pages
10h 14m
English
Wrox
Content preview from ASP.NET 4 24-Hour Trainer

Chapter 11B. Master Pages in MVC

In this lesson I show you how to use master pages in an ASP.NET MVC application.

MASTER PAGES

To create a new master page, select the Add New Item option from the Project menu to open the Add New Item dialog box. Select the MVC 2 View Master Page template from the dialog box and click the Add button (see Figure 11B-1).

FIGURE 11B-1

Figure 11B.1. FIGURE 11B-1

This is the markup that is generated in the default master page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>
        <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
<body>
    <div>
        <asp:ContentPlaceHolder ID="MainContent" runat="server">
        </asp:ContentPlaceHolder>
    </div>
</body>
</html>

The default master page looks like a regular page except that it includes an @Master directive at the top of the page instead of an @Page directive, it includes a ContentPlaceHolder control in its head element and it includes a ContentPlaceHolder control in its body element. The ContentPlaceHolder controls are used by the pages that inherit from the master page.

Note

A master page can have as many ContentPlaceHolder controls as your design requires. You are not limited to the two ContentPlaceHolder

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 4 24-Hour Trainer

ASP.NET 4 24-Hour Trainer

Toi B. Wright
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470596913Purchase bookExamplesErrata