Nested master page

Source: Internet
Author: User

6.4Nested master page

The so-called "nesting" is a set of large container sets and small containers. A nested master page is used to create a large master page that contains another small master page. As shown in Figure 6.5, the nested master page is displayed.

Figure 6.5 nested master page

You can use nested master pages to create componentized master pages. For example, a large website may contain a master page that defines the appearance of the site. Then, different website content partners can define their parent and child versions, which reference the master pages of the website, define the content appearance of the partner accordingly.

Example 6.1 create a simple nested master page (example location: Mr "SL" 06 "01)

The following example mainly uses a simple nested master page example to deepen your understanding of the nested master page. Run the program, as shown in sample 6.6.

(Click to view the big chart) Figure 6.6 nested master page

The main steps for program implementation are:

(1) create a new website and name it 01.

(2) Under the solution of the website, right-click the website name and select the "Add new project" command from the shortcut menu to open the "Add new project" dialog box, first, add two master pages, named mainmaster (master page) and submaster (Child master page), and then add a web form named default. aspx and use it as the content page of submaster.

The page shown in 6.6 consists of the master page (mainmaster), submaster page (submaster), and content page (default. the content on the master page is mainly the public part of the page. The master page is nested with the parent page, and the content page is bound to the parent page.

(3) the method for building a master page is the same as that for creating a common master page. Because the master page is nested with a parent page, you must set a contentplaceholder control at an appropriate location to implement a placeholder. The Design Code of the master page is as follows:

<% @ Master language = "C #" autoeventwireup = "true" codefile ="
Mainmaster. master. cs "inherits =" mainmaster "%>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> master page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Table Style = "width: 759px; Height: 758px" cellpadding = "0" cellspacing = "0">
<Tr>
<TD style = "background-image: URL (image/baner.jpg );
Width: 759px; Height: 153px ">
</TD>
</Tr>
<Tr>
<TD style = "width: 759px; Height: 498px" align ="
Center "valign =" Middle ">
<Asp: contentplaceholder id = "maincontent" runat = "server">
</ASP: contentplaceholder>
</TD>
</Tr>
<Tr>
<TD style = "background-image: URL (image/3.jpg );
Width: 759px; Height: pixel PX ">
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

(4) The child master page uses the. Master extension. Its code consists of two parts: the code header and content control. Compared with a normal master page, the Child master page does not contain web elements such as <HTML> and <body>. A masterpagefile attribute is added to the Code header of the Child master page to set the path of the master page of the nested child master page. By setting this attribute, the nesting between the master page and the Child master page is realized. The contentplaceholder Control declared in the Content Control of the Child homepage is used to implement placeholder for the content page. The Design Code of the Child Mother page is as follows:

<% @ Master language = "C #" autoeventwireup = "true" codefile ="
Submaster. master. cs "inherits =" submaster "masterpagefile = "~ /Mainmaster. Master "%>
<Asp: Content ID = "content1" contentplaceholderid = "maincontent" runat = "server">
<Table Style = "background-image: URL (image/2.jpg); width: 759px; Height: 498px">
<Tr>
<TD align = "center" valign = "Middle">
<H1> parent version page </TD>
<TD align = "center" valign = "Middle">
<Asp: contentplaceholder id = "subcontent" runat = "server">
</ASP: contentplaceholder>
</TD>
</Tr>
</Table>
</ASP: content>

(5) the construction method of the content page is consistent with that of the common content page. Its code consists of two parts: the code header Declaration and the content control. Because the content page is bound to a child mother page, the masterpagefile attribute in the Code header must be set to the path of the Child Mother page. The Design Code of the content page is as follows:

<% @ Page Language = "C #" masterpagefile = "~ /Submaster. Master "autoeventwireup =" true"
Codefile = "default. aspx. cs" inherits = "_ default" Title = "untitled page" %>
<Asp: Content ID = "content1" contentplaceholderid = "subcontent" runat = "server">
<Table Style = "width: 451px; Height: 391px">
<Tr>
<TD>
<H1> content page </TD>
</Tr>
</Table>
</ASP: content>

Note: When creating a nested master page, Visual Studio 2005 only supports editing the master page, but does not support the child master page or content page. That is to say, You must create a parent page and content page in code mode.

6.5Controls and properties for accessing the master page

There are some restrictions on referencing attributes, methods, and controls on the master page on the Content Page. The rules for attributes and methods are: if they are declared as public members on the master page, they can be referenced, including public attributes and public methods. When referencing controls on the master page, there is no such restriction that only public members can be referenced.

6.5.1Use the master. findcontrol () method to access the control on the master page

In the content page, the page object has a public property master, which can be used to reference the master page base class masterpage. The masterpage in the master page is equivalent to a common ASP. therefore, you can use the masterpage object to access each sub-object on the master page. However, the controls on the master page are protected and cannot be accessed directly, you must use the findcontrol method of the masterpage object.

For example, 6.2 access the control on the master page (for example, Mr "SL" 06 "02)

The following example uses the findcontrol method to obtain the label control used to display the system time on the master page. Run the program, as shown in sample 6.7.

(Click to view the big chart) Figure 6.7 nested master page

The main steps for program implementation are:

(1) create a website. First, add a master page named masterpage. Master by default, and then add a web form named default. aspx as the content page of the master page.

(2) Add a label control on the master page and content page respectively. The ID attribute of the label control on the master page is labmaster, which is used to display the system date. The ID attribute of the label control on the Content Page is labcontent, which is used to display the label control value on the master page.

(3) In the page_load event of the masterpage. maste master page, the code for displaying the current system date in the label control of the master page is as follows.

Protected void page_load (Object sender, eventargs E)
{
This. labmaster. Text = "today is" + datetime. Today. Year + "year"
+ Datetime. Today. Month + "month" + datetime. Today. day + "day ";
}

(4) In the page_loadcomplete event on the default. aspx content page
The following code shows the label control value on the master page.

Protected void labcontent_prerender (Object sender, eventargs E)
{
Label mlable1 = (Label) This. master. findcontrol ("labmaster ");
This. labcontent. Text = mlable1.text;
}

Note: Before the page_load event of the master page is triggered, the page_load event of the content page has been triggered. Therefore, it is difficult to access controls on the master page from the content page. Therefore, this example uses the page_loadcomplete event added by ASP. net2.0 and the findcontrol () method to obtain the control of the master page. The page_loadcomplete event is triggered within the lifecycle and when the web page is loaded. Of course, this function can also be completed under the prerender event of the label control.

6.5.2Reference the @ mastertype command to access the attribute on the master page

To reference the attributes and methods on the master page, you must use the mastertype command on the Content Page to forcibly type the master attributes on the Content Page, the mastertype command is used to create a strong type reference for the master page related to the content page. In addition, when setting the mastertype command, you must set the virtualpath attribute to specify the master page storage address related to the content page.

Example 6.3 access the properties on the master page (example location: Mr "SL" 06 "03)

The following example uses the mastertype command to reference the public attributes of the master page and assign the welcome to the public attributes of the master page. Run the program, as shown in sample 6.8.

(Click to view the big chart) Figure 6.8 nested master page

The main steps for program implementation are:

(1) For program development steps, see example 06 "02.

(2) define a string type public attribute mvalue on the master page. The Code is as follows:

Public partial class masterpage: system. Web. UI. masterpage
{
String mvalue = "";
Public String mvalue
{
Get
{
Return mvalue;
}
Set
{
Mvalue = value;
}
}
// Other code
}

<% = Mvalue %> is displayed in the parent layout. The Code is as follows:

// Other code
<TD style = "background-image: URL (image/baner.jpg); Height: 153px" align = "center">
<Asp: Label id = "labmaster" runat = "server"> </ASP: Label>
<% = This. mvalue %>
</TD>
// Other code

(3) added <% @ mastertype %> in the Code header settings on the Content Page, and set the virtualpath attribute in it, used to set the URL of a strongly typed master page. The Code is as follows:

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup ="
True "codefile =" default. aspx. cs "inherits =" _ default "Title =" untitled page "%>
<% @ Mastertype virtualpath = "~ /Masterpage. Master "%>
<Asp: Content ID = "content1" contentplaceholderid ="
Contentplaceholder1 "runat =" server ">
<Table align = "center">
<Tr>
<TD style = "width: 86px; Height: 21px;">
<Asp: Label id = "labcontent" runat = "server"
Width = "351px"> </ASP: Label> </TD>
</Tr>
</Table>
</ASP: content>

(4) In the page_load event of the content page, the public attributes in the master page are referenced through the master object, and the welcome words are assigned to the Public attributes on the master page. The Code is as follows:

Protected void page_load (Object sender, eventargs E)
{
Master. mvalue = "welcome ";
}

6.6Practices and exercises

Create a web application

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.