Access the objects on the master page on the ASP. NET content page, asp.net object

Source: Internet
Author: User

Access the objects on the master page on the ASP. NET content page, asp.net object

The master page function is provided starting with ASP. NET2.0. A master page consists of one master page and multiple content pages. The main function of a master page is to create the same layout and style for pages in ASP. NET applications. The usage of a master page is similar to that of a common page, where files or graphics, HTML controls, Web controls, and post-code can be placed.

A master page is only a page template. A single master page cannot be accessed by users. Master pages and content pages have strict mappings. The number of ContentPlaceHolder controls contained in the master page. Therefore, you must set the corresponding Content control on the Content Page.

After reviewing the concept of the master page, I will summarize it today.How to access objects in the associated master page on a common page(For example, the value of the Control ):

We know that the Page object hasPublic attribute Master.Master page base class MasterPageMasterPage is equivalent to the Page object in a common ASP. NET Page. Therefore, you can use the MasterPage object to access each sub-object on the master page.

The specific program example is as follows:

In an ASP. NET empty website project, add a master page MasterPage1.Master and a common page Default. Then, add a Label button on the master page and content page. The ID attribute of the Label control on the master page is labMaster, which is used to display the current system date. The ID attribute of the Label control on the Content Page is labContent, which is used to display (ACCESS) the Label control value on the master page.
Add the following code to the Page_Load event on the MasterPage1.Master master page:

1 protected void Page_Load (object sender, EventArgs e) 2 {3 this. labMaster. text = "today is" + DateTime. today. year + "Year" + DateTime. today. month + "Month" + DateTime. today. day + "Day"; 4}

On the Default. aspx content pagePage_LoadCompleteAdd the following code to the event:

1 protected void Page_Complete (object sender, EventArgs e) 2 {3 if (! This. isPostBack) 4 {5 // controls used to access the template page on a common page; 6 Label masterLabel = this. master. findControl ("labMaster") as Label; 7 this. labContent. text = masterLabel. text; 8} 9}

After running the program, the Label. text Value of the master page is displayed in the Label control of the Content Page.
Note that the Page_Load event of the content page is first triggered by the Page_Load event of the master page. Therefore, the Page_LoadComplete event is used here. The Page_LoadComplete event is triggered during the lifecycle and when the page load ends.

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.