To use a master page on another page, you must add the MasterPageFile attribute to the page directive:
Setting the MasterPageFile feature is not enough to turn a normal page into a content page. The content page must define the content to insert one or more ContentPlaceHolder controls (and write the code needed for those controls). Because the master page already provides a shell, attempting to include elements such as .
To provide content for ContentPlaceHolder, you need to use another special control called content. ContentPlaceHolder and Content controls have a one-to-one relationship. For each ContentPlaceHolder in the master page, the content page provides a corresponding content control (unless you are not prepared to provide anything for that area). asp.net corresponds to the ContentPlaceHolder ID and the Content.contentplaceholderid property of the corresponding Content control.
<%@ Page title= "" Language= "C #" masterpagefile= "~/chapter16/sitetemplate.master"
Autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "Chapter16_default"%>
<asp:content id= "Content1" contentplaceholderid= "ContentPlaceHolder1" runat= "Server" >
<p class= "Code" style= "margin:0in 0.1in 0pt 0in" >
<span style= "FONT-SIZE:10PT; Font-family:thesansmonoconnormal ">far out in the uncharted
Backwaters of the unfashionable end of the Western spiral arm of the Galaxy lies
A small unregarded yellow sun.</span></p>
</asp:Content>
<asp:content contentplaceholderid= "titlecontent" id= "Content2" runat= "Server" >
Custom title</asp:content>
To better understand how the master page works, it's worth looking at the content page by tracking (adding trace=true to the page directive). This way you can understand the hierarchy of controls. You'll find ASP.net first creates a control object for the master page, including ContentPlaceHolder (which acts as a container), and then it adds the control of the content page to the ContentPlaceHolder.
If you need to dynamically configure a master page or content page, you can respond to page.load events in any of the classes. Sometimes you might use initialization code in both the master page and the content page. In this case, it is important to understand the order in which each event occurs. asp.net first create the master page control, and then add the child controls for the content page. It then triggers the Page.Init event for the master page, followed by the Page.Init event for the content page. This is the same procedure for Page.load events. (If there is a conflict, the customization of the content page overrides the modifications made at the same stage of the master page)
Default Content
The master page definition ContentPlaceHolder can contain the default content that is used when the content page does not provide the corresponding content control.
Content pages cannot use only part of the master page default content or edit this part only. This is not possible because the default content is saved in the master page rather than in the content page. All, either fully, or replace it all.
Master pages with table and CSS layouts
HTML uses a flow based layout. This means that as the content increases, the page is organized and some other content is pushed aside. Such a layout makes it difficult to obtain the expected results of the master page. If you're not careful, you'll spoil the perfect layout, and a lot of information inserted into the <Content> tag will mess up the page structure.
To control these issues, most master pages use HTML tables or CSS positioning to control the layout.
When using a table, the basic principle is to decompose the entire page or part of the page into rows and column. You can then add ContentPlaceHolder to a cell to ensure that the other content is aligned as expected.
Using CSS positioning, the basic idea is to put content into <div> tags, and then use absolute coordinates to control the position of <div> or let them float on one side of the page, and finally you can put ContentPlaceHolder into <div> Label. (There are many good examples based on CSS layouts in http://www.csszengarden.com and http://www.bluerobot.com/web/layouts)
The following example demonstrates how to use a master page to create a traditional Web application that contains a header, footer, and navigation bar that are defined by a table:
<%@ Master language= "C #" autoeventwireup= "true" codefile= "TableMaster.master.cs"
inherits= "Tablemaster"%><