master pages tutorial master pages tutorial - contributed by Nihal Singh

.NET Tutorial > Master Pages

Introduction to Master Pages

ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages in your website. A master page can serve as a template for one or more web forms

Advantages of using master pages

  • They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
  • They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
  • They provide an object model that allows you to customize the master page from individual content pages

Important points about master pages:

  • A master page is defined with the file extension .master
  • Master pages contain a @ Master directive.
  • ContentPlaceHolder controls is only available for Master Pages
  • To enable pages to insert content into a master page, add one or more ContentPlaceHolder controls
  • You must have a Content control on a content page. one Content control is
  • Added to the page for each ContentPlaceHolder control inside the master page.
  • An ASP.NET page that uses Master Pages is called content pages.

Referencing Master Page Properties and Controls from Content Pages

You can easily access the property and controls that is available on master pages.

The basic steps to reference master page properties from a content page are as follows:

  • 1. Create a property in the master page code-behind file.
  • 2. Add the @ MasterType directives to the .aspx content page and use the attribute “VirtualPath”.
  • 3. Reference the master page property from the content page using the syntax


Creating a Property in the Master Page

// txtName is the ID of TextBox in Master Page

public String EmpName

            get { return txtName.Text; }

            set { txtName.Text = value; }


Connecting to Master Page Properties from Content Pages

You must add the @ MasterType declaration to the .aspx content page to reference master properties in a content page.

<%@ MasterType VirtualPath="~/MasterPageName.master" %>
Now you can reference properties in the master page using the Master class.


Referencing Controls in the Master Page

You can also reference a control on the master page from a content page through the Master.FindControl method.
The return type of Master.FindControl is Control.So you have to cast it to correct Control Type

Label EmpName = (Label)Master.FindControl("EmpNameTextBox");

Write your comment - Share Knowledge and Experience

Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring