System.Web.UI.MasterPage Class

Acts as a template and merging container for pages that are composed only of System.Web.UI.WebControls.Content controls and their respective child controls.

See Also: MasterPage Members


public class MasterPage : UserControl


A master page functions as a template container and merging page for content pages in your ASP.NET Web application. Master pages provide a convenient way to share structure and content across a set of content pages. You use content placeholders to define the sections of the master page to replace with content from the content pages.

When you use a master page and its related content pages, you add the required XHTML document tags (such as html, head, and body) only to the master page and no longer create your other .aspx files (ASP.NET pages) as stand-alone Web pages. The content pages define the content to insert into the placeholders in the master page.

When an HTTP request is made for a page at run time, the master page and content pages are combined into a single class with the same name as the content pages. The resulting compiled, merged class derives from the System.Web.UI.Page class.

A master page can contain direct markup and server controls, as well as container controls. Every element that is placed in the master page outside of a System.Web.UI.WebControls.ContentPlaceHolder control is rendered on all pages that result from merging the master page and content pages.

Each content page that is related to the master page must reference the master page in a MasterPageFile attribute of its @ Page directive. Content pages can contain only a @ Page directive and one or more System.Web.UI.WebControls.Content controls. All of your page text, markup, and server controls must be placed within System.Web.UI.WebControls.Content controls. You identify the System.Web.UI.WebControls.ContentPlaceHolder control of a master page that a System.Web.UI.WebControls.Content control is associated with by setting the System.Web.UI.WebControls.Content.ContentPlaceHolderID property of the System.Web.UI.WebControls.Content control.

At run time, the dynamic content from each System.Web.UI.WebControls.Content control in the requested page is merged with the master page in the exact location of the related System.Web.UI.WebControls.ContentPlaceHolder control. Any other markup and controls in the master page are unaffected. Event handlers can be defined in both the master class and on the content page. For more information, see Events in Master and Content Pages.

The System.Web.UI.MasterPage class is associated with files that have a .master extension. These files are compiled at run time as System.Web.UI.MasterPage objects and are cached in server memory.

The master page is made available to the content page through the Page.Master property of the base System.Web.UI.Page class. The Page.Master property returns the instance of the master page; however, it is typed as the base System.Web.UI.MasterPage class. To access controls, properties, and functions of the master page, the Page.Master property can be cast to a System.Web.UI.MasterPage object. The class name of the master page is defined using the ClassName attribute of the @ Master directive.


Files with .master extensions are not served to a browser.

The directives that are valid on a master page are the same as those that are available on a System.Web.UI.UserControl object. They can include the following attributes:

Master page directives do not override the directives on individual content pages.

Master pages are most often created declaratively. If you want to create a master page programmatically, derive directly from the System.Web.UI.MasterPage class. In addition to extending the System.Web.UI.MasterPage class, you must create the .master file to visually display the user interface (UI) that is associated with the classes that you have invoked in your source file.


When you create a master page by creating your own class first, you must include all namespaces that are required for the classes that are used by the page.

For more information about master pages, see ASP.NET Master Pages Overview.


Namespace: System.Web.UI
Assembly: System.Web (in System.Web.dll)
Assembly Versions:
Since: .NET 2.0