How to reference the content on the dashboard page on the Content Page

Source: Internet
Author: User

You can writeCodeTo reference properties, methods, and controls on the master page. 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.

Reference the content of the dashboard page
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;
}
}

 

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.