Master Pages)
Http://blog.csdn.net/iiboy/
One of the most prominent disadvantages of ASP. NET 1.x is that it lacks support for page templates. What is lacking is the ability to define "master pages" that can be inherited by other pages. Developers make up for this by using user controls (which can be easily copied between pages) to create pages. In ASP. NET 2.0, this technique is no longer needed because a new feature called "master page" is added. Think about "visual inheritance", and you will understand what the master page is. First, you need to define a master page (including the content you want to display on other pages), and use the ContentPlaceHolder control to define the location where the child page can insert its content. Then, you need to generate the child page-SPX file, which uses (similar to) the following commands to reference the master page:
<% @ Page MasterPageFile = "~ /Foo. master "%>
On the child page, you can use the Content control to fill in placeholders on the master page. When a child page is displayed in a browser, the content displayed is a perfect combination of the content defined on the master page and child page.
<% @ Master %>
<Html>
<Body leftmargin = "0" topmargin = "0" rightmargin = "0"
Bottommargin = "0" marginheight = "0" marginwidth = "0"
>
<! -- Banner -->
<Table cellspacing = "0" cellpadding = "0"
Style = "background-image: url ('images/stripes.gif ');
Background-repeat: repeat-x, repeat-y "width =" 100%"
>
<Tr> <td align = "center">
<Span style = "font-family: verdana; font-size: 36pt;
Font-weight: bold; color: white"
>
Master Pages
</Span> <br>
<Span style = "font-family: verdana; font-size: 10pt;
Font-weight: normal; color: white"
>
This banner provided by Master. master
</Span>
</Td> </tr>
</Table>
<! -- Placeholder for content between banner and footer -->
<Form id = "Content">
<Asp: ContentPlaceHolder ID = "Main" RunAt = "server"/>
</Form>
<! -- Footer -->
<Table width = "100%"> <tr> <td align = "center">
<Span style = "font-family: verdana; font-size: 8pt; color: red">
Copyright (c) 2004 by Me Inc. All rights reserved <br>
This footer provided by Master. master
</Span>
</Td> </tr> </table>
</Body>
</Html>
Master. master
<% @ Page MasterPageFile = "~ /Master. master "%>
<Asp: Content ContentPlaceHolderID = "Main" RunAt = "server">
<Table width = "100%" height = "256px"> <tr> <td align = "center">
<H2> This content provided by Subpage. aspx </Td> </tr> </table>
</Asp: Content>
Code 1 Subpage. aspx
The application shown in code 1 uses the master page to define the headers and footers that appear on each page. The child page inserts the Content control into the ContentPlaceHolder of the master page to insert Content between the header and footer. Pay attention to the matched ID and ContentPlaceHolderID, as well as the @ Master command on the Master page.
The master page is fully supported in the ASP. NET object model. System. web. UI. the Page class has a new property named Master, which enables the child Page to reference its Master Page and the controls defined in it programmatically. Master pages can be nested and contain default content that can be rewritten on subpages.
<Asp: ContentPlaceHolder ID = "Main" RunAt = "server">
This is default content that will appear in subpages unless explicitly overridden
</Asp: ContentPlaceHolder>
In addition, the application can specify the default master page in Web. config, as shown below:
<Configuration>
<System. web>
<Pages masterPageFile = "~ /Foo. master "/>
</System. web>
</Configuration>
A single sub-page is free to override the default master page and specify its own master page.
The most exciting part is the support for master pages in Visual Studio 2005. When a child page is loaded, IDE displays the gray, read-only version of the content defined in the master page, and the full color and fully editable version of the content defined in the Child page. It is easy to distinguish between the two. to edit the content of the master page, you only need to open the master page in IDE.
For more details about the master page, see related articles.