Obtain the Control ID in masterpage

Source: Internet
Author: User
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.

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.