Introduction to .NET Framework 3.0/An Overview of Master Pages

From Wikibooks, open books for an open world
< Introduction to .NET Framework 3.0
Jump to: navigation, search

Masterpages were introduced for the first time in .NET framework 2.0 in order to create an application in which multiple files have similar content. The content for which masterpages have been created include headers, trailers, etc. For sites which have a common header for all the pages, it is difficult and hectic to ensure that the same header has been inserted into the code of each page and to ensure uniformity in text alignment, etc. To overcome this drawback, we use master pages. In .NET 1.1, cascading style-sheets were used to perform this function. However, in .NET 3.0, these stylesheets were replaced by masterpages.

Introduction[edit]

The syntax used to denote a masterpage is

<%@ Master Language="C#" %>
<%@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>


This specifies that the page is a masterpage and is given at the start of the page.

The ASP.NET page which inherits content from a masterpage has the following tag in it.

<%@ Page Language="VB" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page" %>

Use of Master Pages[edit]

Creating a Master Page[edit]

  1. Create a New Project
  2. Add a master page to the project. This could be done by clicking 'Add New Item' and selecting 'Masterpage' from the list of items displayed.
  3. Open the masterpage and enter the contents to be displayed just above the ContentPlace Holder object.
  4. Right-click on the project and click 'Add New Item'. Select 'Web Page' and check 'Select Master Page' option and click 'Ok'. The project folder dialog box will be displayed. Select the masterpage file (.master) from the list of files displayed and click 'Ok'. The file gets created and you will observe that a 'content' object gets created by default. This tag is created when a masterage is selected. If no masterpages are required in the page then this tag can be removed.
  5. Run the file. You will observe that the content specified on top of the ContentPlaceHolder in the Masterpage file appears on the Web page.

Nested Masterpages[edit]

It is also possible to nest one masterpage inside another. The designer doesn't support nesting of master pages; however, two or more masterpages could be nested at the code level.

SiteMaster.master[edit]

The SiteMaster page is made up exclusively of the Content control.

<asp:contentplaceholder id="SiteContentPlaceHolder" runat="server">
<h1>Site Master Page</h1></asp:contentplaceholder>

Section Master[edit]

Section masterpages inherit from the site master. Syntax of a site master page is given below.

<asp:contentplaceholder id="SiteContentPlaceHolder" runat="server">
<h1>Site Master Page</h1></asp:contentplaceholder>

Scoping Masterpages[edit]

Masterpages could be specified either at the page or folder or application level.

Criticism of Masterpages[edit]

One complaint against master pages is that the head HTML tag is defined in the master page. This element must be shared across all pages that take their layout from the master. Several options are available to work around this issue, including modifying the head content at Runtime using Server-side code. The title, however, is freely editable via the title attribute in the master page file declaration at the top of a content page. It is actually possible to place a ContentPlaceHolder control inside the head section of the master page, although Visual Studio 2005 will generate warnings. Unfortunately, markup placed inside the associated Content control is rendered as a literal. The HtmlHead object contains specialized parsing routines to recognize elements such as link and script. This parsing code is not applied to children of a child ContentPlaceHolder control. However, there is a workaround for this. If each link and script control uses the runat="server" attribute, then they will be parsed by ContentPlaceHolder as individual HtmlGenericControls. Code can then be written to iterate through each HtmlGenericControl and replace it with the appropriate control type (HtmlLink, HtmlMeta, etc.) after performing attribute mapping.[citation needed]

References[edit]

  1. http://www.codersource.net/asp_net_master_pages_whidbey.aspx