Home | HTML | Asp.Net | HTML Probe | .Net Probe
  Contact Us |Sitemap

Master Page in Asp.net

  Skip Navigation LinksHome>Asp Home>Master Page

A Master page enables you to create a page layout that can be used by any number of ASP.NET pages in your application. The file extension of master page is .master, whereas content pages use the .aspx file extension. You cannot view master page directly in the browser. A master page can include the header, navigation, and footer sections used across the Web application. The content page then contains all the page content except for the master page’s elements. At runtime, the ASP.NET engine combines these elements into a single page for the end user.

How to create a master page

To create a master page in Visual Studio, select Website → Add New Item from the menu. Select Master Page, give it a filename (such as MasterPage.master), and click Add.

The .aspx page contains the @page directive whereas the master page replaces the @page directive to @master directive as shown in the following example.

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

The @ Master directive contains the following elements:
  • HTML elements: A developer can use the HTML elements in a Master page such as img> for displaying the image, table> to set the layout of the page etc. these tags are also called ASP.NET elements.

  • ContentPlaceHolder Control: A Master page should contain at least one ContentPlaceHolder. ContentPlaceHolder provides a modifying space for a content page. Each content page references a single master page and acquires its layout and content. In other words, the content page fills in the missing pieces that the master page doesn’t define.

Applying Master Pages through a Configuration File :

It’s worth noting that you can also apply a master page to all the pages in your website at once using the web.config file. All you need to do is add the <pages> attribute and set its masterPageFile attribute, as shown here:



       <pages masterPageFile ="MasterPage.master"/>     



Working with the Master Page Title and Source code

The Master Page file contains the <title> tag within the <head> tag. By default all the content pages automatically use the title that is declared in the master page. However, the title specified in the master page can be overridden in the content page by using the <title> attribute in the @Page directive in the content page.

A master page contains <head>, <html> and <body> elements. The source code for the content page is different from the normal page, because the content page can not define anything which is already defined in the master page such as <head>, <html>, <body> element.

Nesting Master Pages

You can nest master pages so that one master page uses another master page. It could allow you to standardize your website to different degrees. You can create different section in your website where each section defines separate navigation control.

To create a nest master page select the master page templates click the chechbox of the "select master page" before add the page. After clicked the "add" button a another template will open to select a master page.

After clicked the "Add" button Select the Master Page as shown below :

Example of the Master Page:

Code View

Design View

DMCA.com Protection Status
Design & Maintain By solvingera group| solvingera.com © 2013-2014 | all right reserved |Privacy Policy | Back to top