Asp. NET content pages to access the objects in the master page

Source: Internet
Author: User

At the beginning of asp.net2.0, the functionality of the master page is provided. A master page consists of a master page and multiple content pages. The primary function of a master page is to create the same layout and interface style for pages in an ASP. master pages are used like normal pages where you can place files or graphics, any HTML control and Web controls, post code, and so on.

A master page is simply a page template, and a separate master page cannot be accessed by the user. Master pages and content pages have a strict correspondence relationship. How many ContentPlaceHolder controls are included in the master page, you must also set the content control that corresponds to the contents page.

After reviewing the concept of master pages, I am mainly summarizing how to access objects in the master page associated with it in a normal page (for example, the value of a control):

We know that the Page object has a public property, Master, that can implement a reference to the base class masterpage of the master page , and masterpage the page object in the normal ASP. Therefore, you can use the MasterPage object to implement access to individual child objects in the master page.

the specific program examples are as follows:

In an ASP. NET empty Web site project, add a master page masterpage1.master and a normal page default. Then add a label button to the master page and the content page, respectively. The ID property of the Label control in the master page is Labmaster, which is used to display the current system date, and the ID property of the Content page's label control is Labcontent, which is used to display (access) the Label control value in the master page.
Add the following code to the Page_Load event of the Masterpage1.master master page:

1 protected voidPage_Load (Objectsender, EventArgs e)2     {3          This. Labmaster.text ="today is"+ DateTime.Today.Year +"years"+ DateTime.Today.Month +"Month"+ DateTime.Today.Day +"Day";4}

In the page_loadcomplete event in the Default.aspx content page, add the following code:

1Protectedvoid Page_complete (Objectsender, EventArgs e)2{3 if (! This4  {5 //6 Label Masterlabel = this. Master.findcontrol ( "labmaster  ") as Label; 7 this.labcontent.text = Masterlabel.text;8 }9}       

After you run the program, the value of the master page Label.text is displayed in the label control of the content page.
It is important to note that the Page_Load event of the content page is raised before the Page_Load event of the master page. So the Page_loadcomplete event is used here. Where the Page_loadcomplete event is triggered during the life cycle and at the end of the page load.

Asp. NET content pages to access the objects in the master page

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.