In this tutorial, you learn how to create a new view master page and create a new view content page based on the master page.



Similar documents
MVC :: Passing Data to View Master Pages

MVC :: Passing Data to View Master Pages

Displaying a Table of Database Data (VB)

HTML5 and CSS3 Part 1: Using HTML and CSS to Create a Website Layout

CSS - Cascading Style Sheets

Building A Very Simple Web Site

Further web design: HTML forms

Web Development 1 A4 Project Description Web Architecture

CREATING A NEWSLETTER IN ADOBE DREAMWEAVER CS5 (step-by-step directions)

WHITEPAPER. Skinning Guide. Let s chat Velaro info@velaro.com by Velaro

HTML CSS Basic Structure. HTML Structure [Source Code] CSS Structure [Cascading Styles] DIV or ID Tags and Classes. The BOX MODEL

LAB 1: Getting started with WebMatrix. Introduction. Creating a new database. M1G505190: Introduction to Database Development

Microsoft Expression Web

QQ WebAgent Quick Start Guide

Quick Start Guide. This guide will help you get started with Kentico CMS for ASP.NET. It answers these questions:

Introduction to Web Design Curriculum Sample

Using Style Sheets for Consistency

Website Planning Checklist

Recreate your Newsletter Content and Layout within Informz (Workshop) Monica Capogna and Dan Reade. Exercise: Creating two types of Story Layouts

Dreamweaver CS4 Day 2 Creating a Website using Div Tags, CSS, and Templates

Chapter 2 HTML Basics Key Concepts. Copyright 2013 Terry Ann Morris, Ed.D

UH CMS Basics. Cascade CMS Basics Class. UH CMS Basics Updated: June,2011! Page 1

SPHOL326: Designing a SharePoint 2013 Site. Hands-On Lab. Lab Manual

Yandex.Widgets Quick start

Website Login Integration

CSS Techniques: Scrolling gradient over a fixed background

Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

Web Design Basics. Cindy Royal, Ph.D. Associate Professor Texas State University

Website Development. 2 Text. 2.1 Fonts. Terry Marris September We see how to format text and separate structure from content.

How to code, test, and validate a web page

Using an external style sheet with Dreamweaver (CS6)

WordPress websites themes and configuration user s guide v. 1.6

Create Webpages using HTML and CSS

PLAYER DEVELOPER GUIDE

Madison Area Technical College. MATC Web Style Guide

Fireworks 3 Animation and Rollovers

Outline of CSS: Cascading Style Sheets

MAGENTO THEME SHOE STORE

darlingharbour.com Content Management System Tenant User Guide

A send-a-friend application with ASP Smart Mailer

ACE: Dreamweaver CC Exam Guide

Page Editor Recommended Practices for Developers

Mail Merge Creating Mailing Labels 3/23/2011

Creating Web Pages with Microsoft FrontPage

Kentico CMS 7.0 Tutorial ASPX

Creating a Resume Webpage with

Web Design with Dreamweaver Lesson 4 Handout

Basic Website Maintenance Tutorial*

Producing Standards Based Content with ToolBook

ADOBE DREAMWEAVER CS3 TUTORIAL

ICE: HTML, CSS, and Validation

HOW TO CREATE AN HTML5 JEOPARDY- STYLE GAME IN CAPTIVATE

Designing HTML s for Use in the Advanced Editor

Website Builder Overview

Adobe Dreamweaver CC 14 Tutorial

Using JCPS Online for Websites

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

MASTERTAG DEVELOPER GUIDE

About SharePoint Server 2007 My Sites

Web Design & Development - Tutorial 04

Cascading Style Sheets (CSS)

Themes and Templates Manual FOR ADVANCED USERS

CS412 Interactive Lab Creating a Simple Web Form

Web Development CSE2WD Final Examination June (a) Which organisation is primarily responsible for HTML, CSS and DOM standards?

Designing for Magento

Kentico CMS 7.0 User s Guide. User s Guide. Kentico CMS

Embedding a Data View dynamic report into an existing web-page

Using Adobe Dreamweaver CS4 (10.0)

Microsoft Office PowerPoint Creating a new presentation from a design template. Creating a new presentation from a design template

HTML TIPS FOR DESIGNING

Microsoft Expression Web Quickstart Guide

OPENTABLE RESERVATION WIDGET GETTING STARTED ADD RESERVATIONS TO YOUR WEBSITE

Designer s Guide. Designer Guide for AbleCommerce Page 1

SF Developer Guidelines V 9.05 June D3.COM Pty Ltd

Create a Simple Website. Intel Easy Steps Intel Corporation All rights reserved.

HTML, CSS, XML, and XSL

Kentico CMS, 2011 Kentico Software. Contents. Mobile Development using Kentico CMS 6 2 Exploring the Mobile Environment 1

Google Sites: Creating, editing, and sharing a site

How to create pop-up menus

Creating Electronic Portfolios using Microsoft Word and Excel

Kentico CMS 5.5 User s Guide

Kentico CMS User s Guide 5.0

Oracle Business Intelligence Publisher: Create Reports and Data Models. Part 1 - Layout Editor

ANIMATED HEADER IMAGE WITH IMAGE HEADER SLIDESHOW (FL_HEADER_SLIDE)

Guide to PDF Publishing

NUI Galway Web Training Presentation

Interspire Website Publisher Developer Documentation. Template Customization Guide

Making a Web Page with Microsoft Publisher 2003

Transcription:

MVC :: Creating Page Layouts with View Master Pages In this tutorial, you learn how to create a common page layout for multiple pages in your application by taking advantage of view master pages. You can use a view master page, for example, to define a two-column page layout and use the two-column layout for all of the pages in your web application. You also can take advantage of view master pages to share common content across multiple pages in your application. For example, you can place your website logo, navigation links, and banner advertisements in a view master page. That way, every page in your application would display this content automatically. In this tutorial, you learn how to create a new view master page and create a new view content page based on the master page. Creating a View Master Page Let s start by creating a view master page that defines a two-column layout. 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 1 Adding a view master page You can create more than one view master page in an application. Each view master page can define a different page layout. For example, you might want certain pages to have a two-column layout and other pages to have a three-column layout. A view master page looks very much like a standard ASP.NET MVC view. However, unlike a normal view, a view master page contains one or more <asp:contentplaceholder> tags. The <ContentPlaceHolder> tags are used to mark the areas of the master page that can be overridden in an individual content page.

For example, the view master page in Listing 1 defines a two-column layout. It contains two <ContentPlaceHolder> tags. One <ContentPlaceHolder> for each column. Listing 1 Views\Shared\Site.master <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site.Master.vb" Inherits="MvcApplication1.Site" %> <!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 id="head1" <title></title> <style type="text/css"> html { background-color:gray; }.column { float:left; width:300px; border:solid 1px black; margin-right:10px; padding:5px; background-color:white; min-height:500px; } </style> <asp:contentplaceholder ID="head" </head> <body>

<h1>my Website</h1> <div class="column"> <asp:contentplaceholder ID="ContentPlaceHolder1" </div> <div class="column"> <asp:contentplaceholder ID="ContentPlaceHolder2" </div> </body> </html> The body of the view master page in Listing 1 contains two <div> tags that correspond to the two columns. The Cascading Style Sheet column class is applied to both <div> tags. This class is defined in the style sheet declared at the top of the master page. You can preview how the view master page will be rendered by switching to Design view. Click the Design tab at the bottom-left of the source code editor (see Figure 2). Figure 2 Previewing a master page in the designer

Creating a View Content Page After you create a view master page, you can create one or more view content pages based on the view master page. For example, you can create an Index view content page for the Home controller by right-clicking the Views\Home folder, selecting Add, New Item, selecting the MVC View Content Page template, entering the name Index.aspx, and clicking the Add button (see Figure 3). Figure 3 Adding a view content page

After you click the Add button, a new dialog appears that enables you to select a view master page to associate with the view content page (see Figure 4). You can navigate to the Site.master view master page that we created in the previous section. Figure 4 Selecting a master page After you create a new view content page based on the Site.master master page, you get the file in Listing 2. Listing 2 Views\Home\Index.aspx <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="MvcApplication1.Index" %> <asp:content ID="Content1" ContentPlaceHolderID="head" <asp:content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" <asp:content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Notice that this view contains a <asp:content> tag that corresponds to each of the <asp:contentplaceholder> tags in the view master page. Each <asp:content> tag includes a ContentPlaceHolderID attribute that points to the particular <asp:contentplaceholder> that it overrides. Notice, furthermore, that the content view page in Listing 2 does not contain any of the normal opening and closing HTML tags. For example, it does not contain the opening and

closing <html> or <head> tags. All of the normal opening and closing tags are contained in the view master page. Any content that you want to display in a view content page must be placed within a <asp:content> tag. If you place any HTML or other content outside of these tags, then you will get an error when you attempt to view the page. You don t need to override every <asp:contentplaceholder> tag from a master page in a content view page. You only need to override a <asp:contentplaceholder> tag when you want to replace the tag with particular content. For example, the modified Index view in Listing 3 contains only two <asp:content> tags. Each of the <asp:content> tags includes some text. Listing 3 Views\Home\Index.aspx (modified) <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="MvcApplication1.Index" %> <asp:content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" <h1>content in first column!</h1> <asp:content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" <h1>content in second column!</h1> When the view in Listing 3 is requested, it renders the page in Figure 5. Notice that the view renders a page with two columns. Notice, furthermore, that the content from the view content page is merged with the content from the view master page. Figure 5 The Index view content page

Modifying View Master Page Content One issue that you encounter almost immediately when working with view master pages is the problem of modifying view master page content when different view content pages are requested. For example, you want each page in your web application to have a unique title. However, the title is declared in the view master page and not in the view content page. So, how do you customize the page title for each view content page? There are two ways that you can modify the title displayed by a view content page. First, you can assign a page title to the title attribute of the <%@ page %> directive declared at the top of a view content page. For example, if you want to assign the page title Super Great Website to the Index view, then you can include the following directive at the top of the Index view: <%@ Page Title="Super Great Website" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="MvcApplication1.Views.Home.Index" %> When the Index view is rendered to the browser, the desired title appears in the browser title bar:

There is one important requirement that a master view page must satisfy in order for the title attribute to work. The view master page must contain a <head runat= server > tag instead of a normal <head> tag for its header. If the <head> tag does not include the runat= server attribute then the title won t appear. The default view master page includes the required <head runat= server > tag. An alternative approach to modifying master page content from an individual view content page is to wrap the region that you want to modify in a <asp:contentplaceholder> tag. For example, imagine that you want to change not only the title, but also the meta tags, rendered by a master view page. The master view page in Listing 4 contains a <asp:contentplaceholder> tag within its <head> tag. Listing 4 Views\Shared\Site2.master <%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Site2.Master.vb" Inherits="MvcApplication1.Site2" %> <!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> <asp:contentplaceholder ID="head" <title>please change my title</title> <meta name="description" content="please provide a description" /> </head> <body> <meta name="keywords" content="keyword1,keyword2" /> <div> <asp:contentplaceholder ID="ContentPlaceHolder1" </div> </body> </html> Notice that the <asp:contentplaceholder> tag in Listing 4 includes default content: a default title and default meta tags. If you don t override this <asp:contentplaceholder> tag in an individual view content page, then the default content will be displayed. The content view page in Listing 5 overrides the <asp:contentplaceholder> tag in order to display a custom title and custom meta tags.

Listing 5 Views\Home\Index2.aspx <%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site2.Master" AutoEventWireup="false" CodeBehind="Index2.aspx.vb" Inherits="MvcApplication1.Index2" %> <asp:content ID="Content1" ContentPlaceHolderID="head" <title>the Index2 Page</title> <meta name="description" content="description of Index2 page" /> <meta name="keywords" content="asp.net,mvc,cool,groovy" /> <asp:content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Just some content in the body of the page. Summary This tutorial provided you with a basic introduction to view master pages and view content pages. You learned how to create new view master pages and create view content pages based on them. We also examined how you can modify the content of a view master page from a particular view content page.