Masterpage is a new concept in Asp.net 2.0. It 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.
In actual use, the content page must exchange data with masterpage. The following describes a data transfer method.
Add the following statement to the content page:
<%@ MasterType VirtualPath="~/MasterPage.master" %>
The virtualpath value is the matermage path.
Define the public method in materpage
public void SetValue(string s) { this.Label1.Text = s; } public string GetValue() { return this.Label1.Text; }
The setvalue method is used to transmit data from a content page to masterpage.
The getvalue method is used to transmit data from masterpage to the content page.
Call methods in masterpage on the Content Page
If there is no step 2 (create a strong type reference to the master page), you cannot see the method in masterpage on the Content Page.
Protected void button2_click (Object sender, eventargs e) {// pass the value master to masterpage. setvalue ("OK"); // get the value string STR = Master from masterpage. getvalue ();}
Another method for transferring data from masterpage to content page
The above method of transferring data from masterpage to the content page is relatively passive. We can also define events in masterpage and pass data to the content page through events.
Findcontrol
We can call master. findcontrol (Control ID in masterpage) on the Content Page to obtain the control in masterpage.
You can also call master. findcontrol (contentplaceholder Control ID in masterpage). findcontrol (Control ID in content page) to find the control in content page.