Programming Microsoft ASP.NET 4 - Dino Esposito [171]
CodeFile
Indicates the file that contains any source code associated with the master page, and is used for a Web site project.
Inherits
Specifies a code-behind class for the master page to inherit. This can be any class derived from MasterPage.
MasterPageFile
Specifies the name of the master page file that this master refers to. A master can refer to another master through the same mechanisms a page uses to attach to a master. If this attribute is set, you will have nested masters.
The master page is associated with a code file that looks like the following:
public partial class SiteMaster: System.Web.UI.MasterPage
{
protected void Page_Load(Object sender, EventArgs e)
{
...
}
...
}
The @Master directive doesn’t override attributes set at the @Page directive level. For example, you can have the master set the language to Visual Basic and one of the content pages can instead use C#. The language set at the master page level never influences the choice of the language at the content page level.
Likewise, you can use other ASP.NET directives in a master page—for example, @Import. However, the scope of these directives is limited to the master file and does not extend to child pages generated from the master.
The ContentPlaceHolder Container Control
The ContentPlaceHolder control acts as a container placed in a master page. It marks places in the master where related pages can insert custom content. A content placeholder is uniquely identified by an ID. Here’s an example:
A content page is an ASP.NET page that contains only ... In a master page, you define as many content placeholders as there are customizable regions in the page. A content page doesn’t have to fill all the placeholders defined in the bound master. However, a content page can’t do more than just fill placeholders defined in the master. Note A placeholder can’t be bound to more than one content region in a single content page. If you have multiple Specifying Default Content ... The default content is completely ignored if the content page populates the placeholder. The default content is never merged with the custom markup provided by the content page. Note A ContentPlaceHolder control can be used only in a master page. Content placeholders are not valid on regular ASP.NET pages. If such a control is found in an ordinary Web page, a parser error occurs. Writing a Content Page
A content placeholder can be assigned default content that will show up if the content page fails to provide a replacement. Each ContentPlaceHolder control in the master page can contain default content. If a content page does not reference a given placeholder in the master, the default content will be used. The following code snippet shows how to define default content:
Once you have a master page, you think of your actual site pages in terms of a delta from the master. The master defines the common parts of a certain group of pages and leaves placeholders for customizable regions. Each content page, in turn, defines what the content of each region has to be for a particular ASP.NET page. Figure 8-1 shows the first step you take on the way to adding