System.Web.UI Namespace
.NET Framework version 2.0
Acts as a template and merging container for pages that are composed only of Content controls and their respective child controls.
Visibility |
Constructor |
Parameters |
public |
MasterPage |
( )
|
|
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 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 ContentPlaceHolder control is rendered on all pages resulting from the merging of 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 that @ Page directive and one or more Content controls. All of your page text, markup, and server controls must be placed within Content controls. You identify the ContentPlaceHolder control of a master page that a Content control is associated with by setting the ContentPlaceHolderID property of the Content control.
At run time, the dynamic content from each Content control in the requested page is merged with the master page in the exact location of the related ContentPlaceHolder control for each control. All other markup and controls in the master page are unaffected. Event handlers can be present in both the master class and on the content page. For more information, see Events in ASP.NET Master and Content Pages.
The MasterPage class is associated with files that have a .master extension. These files are compiled at run time as MasterPage objects and cached in server memory.
The master pager is made available to the content page through the Master property of the base Page class. The Master property returns the instance of the master page; however, it is typed as the base MasterPage class. To access controls, properties, and functions of the master page, the Master property can be cast to a MasterPage. The class name of the master page is defined using the ClassName attribute of the @ Master directive.
NOTE: Files with .master extensions are not served to a browser.