ASP. NET master page (msdn)

Source: Internet
Author: User

You can use ASP. Net master pages to create consistent la s for pages in applications. A single master page can define the desired appearance and standard behavior for all pages (or a group of pages) in the application. Then, you can create various content pages that contain the content to be displayed. When a user requests a content page, these content pages are merged with the master page to combine the layout of the master page with the content of the content page for output.

The master page is an ASP. NET file with the extension. Master (for example, mysite. Master). It has a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ master command, which replaces the @ page command for the common. ASPX page. This command class looks like the following.
<% @ Master language = "C #" %>

In addition to static text and controls displayed on all pages, the master page also contains one or more contentplaceholder controls. These placeholder controls define the areas where replaceable content appears. On the content page, define the replaceable content.

Content Page
Create content pages to define the content of the placeholder controls on the master page. These content pages are ASP bound to a specific master page. NET page (. (aspx files and optional code hidden files ). Use the masterpagefile attribute that points to the master page to be used to create a binding in the @ page command of the content page. For example, a content page may contain the following @ page command, which binds the content page to the master1.master page. On the content page, add the content control and map these controls to the contentplaceholder control on the master page to create content.

<% @ Page Language = "C #" masterpagefile = "~ /Master. Master "Title =" Content Page 1 "%>
<Asp: Content ID = "content1" contentplaceholderid = "Main" runat = "server">
Main content.
</ASP: content>

On the master page, createContentplaceholderThe control area is displayed as the content control on the new content page.

Displays the remaining content on the master page so that you can view the layout, but the content is light gray because you cannot change the content when editing the content page.

The master page has the following advantages:
1. You can use the master page to process the general functions of the page in a centralized manner, so that you can update the page at only one location.
2. Use the master page to easily create a group of controls and code and apply the results to a group of pages. For example, you can use a control on the master page to create a menu that applies to all pages.
3. The master page allows you to control the layout of the final page on the details by allowing you to control the display of the placeholder control.
4. The master page provides an object model that allows you to customize the parent page from each content page.

Runtime behavior of the master page
During running, the master page is processed as follows:

1. You can request a page by entering the URL of the Content Page.
2. Read the @ page command after obtaining the page. If the command references a master page, the master page is also read. If this is the first request for these two pages, both pages must be compiled.
3. The master page containing updated content is merged into the control tree of the Content Page.
4. The content of each content control is merged into the corresponding contentplaceholder control on the master page.
5. The merged page displayed in the browser.

How to: reference the content of an ASP. NET master page

You can write code on the Content Page to reference attributes, methods, and controls on the master page. However, such references have certain limitations. The rules for attributes and methods are: if they are declared as public members on the master page, they can be referenced. This includes public attributes and public methods. When referencing controls on the master page, there is no such restriction that only public members can be referenced.

Reference public members on the master page
1. Add the @ mastertype command to the content page. In this command, set the virtualpath attribute to the master page location, as shown in the following example: <% @ mastertype virtualpath = "~ /Masters/master1.master "%> This command causes the master attribute of the content page to be strongly typed.
2. write code and use the public members on the master page as a member of the master attribute. In this example, assign the value of the public property of the master page name companyName to a text box on the Content Page.

Reference controls on the master page
Use the findcontrol method to use the return value of the master attribute as the naming container.

The following code example demonstrates how to use the findcontrol method to obtain references to two controls on the master page (one Textbox Control and one label control ). Because the Textbox Control is inside the contentplaceholder control, you must first obtain the reference to contentplaceholder, and then use its findcontrol method to locate the Textbox Control.
Void page_load ()
{
// Gets a reference to a Textbox Control inside
// A contentplaceholder
Contentplaceholder mpcontentplaceholder;
Textbox mptextbox;
Mpcontentplaceholder =
(Contentplaceholder) master. findcontrol ("contentplaceholder1 ");
If (mpcontentplaceholder! = NULL)
{
Mptextbox =
(Textbox) mpcontentplaceholder. findcontrol ("textbox1 ");
If (mptextbox! = NULL)
{
Mptextbox. Text = "textbox found! ";
}
}

// Gets a reference to a label control that not in
// A contentplaceholder
Label mplabel = (Label) master. findcontrol ("masterpagelabel ");
If (mplabel! = NULL)
{
Label1.text = "master page label =" + mplabel. text;
}
}

Author's blog:Http://blog.csdn.net/wangyihust/

Related Article

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.