Programming Microsoft ASP.NET 4 - Dino Esposito [170]
Note
In ASP.NET, a master page is not necessary for any page you add to the project. You can certainly create plain Web pages that don’t import any layout information from the outside. In Microsoft Visual Studio, you are in fact given two options when you choose to add a new Web page to the project—you can add it with or without a master page. In the economy of a real-world site, though, using a master page (or even multiple master pages) is a necessity.
What’s a Master Page, Anyway?
A master page is similar to an ordinary ASP.NET page except for the top @Master directive and the presence of one or more ContentPlaceHolder server controls. In addition, a master page doesn’t derive from Page but has UserControl as its parent class. A ContentPlaceHolder control defines a region in the master page that can be customized in a derived page.
A master page without content placeholders is technically correct and will be processed correctly by the ASP.NET runtime. However, a placeholder-less master fails in its primary goal—to be the super-template of multiple pages that look alike. A master page devoid of placeholders works like an ordinary Web page but with the extra burden required to process master pages.
Here is a simple master page adapted from the master page of the Visual Studio 2010 sample ASP.NET project:
<%@ Master Codebehind=.Site.master.cs. Inherits=.YourApp.SiteMaster. %>
As you can see, the master page looks like a standard ASP.NET page. Aside from the identifying @Master directive, the only key differences are ContentPlaceHolder controls. A page bound to this master automatically picks up the layout and contents of the master and can attach custom markup and server controls to each defined placeholder. The content placeholder element is fully identified by its ID property and normally doesn’t require other attributes.
This is important to note because a content page is not allowed to include any content other than the markup strictly required to fill up a specific content placeholder. I’ll return to this point in a moment.
The @Master Directive
The @Master directive distinguishes master pages from content pages and allows the ASP.NET runtime to properly handle each. A master page file is compiled to a class that derives from the MasterPage class. The MasterPage class, in turn, inherits UserControl. So, at the end of the day, a master page is treated as a special kind of ASP.NET user control.
The @Master directive supports quite a few attributes. For the most part, though, they are the same attributes that we reviewed in Chapter 5, for the @Page directive. Table 8-1 details the attributes that have a special meaning to master pages.
Table 8-1. Attributes of the @Master Directive
Attribute
Description
ClassName
Specifies the name for the class that will be created to render the master page. This value can be any valid class name but should not include a namespace. By default, the class name for a simple.master is ASP.simple_master.
CodeBehind
Indicates the file that contains any source code associated with the master page, and is used for a Web Application Project (WAP).
Note that the CodeBehind attribute is ignored by ASP.NET and simply exists to help Visual Studio edit the file. You can remove it in