How to get the control of the master page

Source: Internet
Author: User

You can use either of the following methods to obtain the content of a master page:

1. Use findcontrol to find the control ID
In this event ~ Because in page_load, the content page is first loaded and then the master page is loaded.
Protected void page_loadcomplete (Object sender, eventargs E)
{
Label2.text = "the current time is" + (master. findcontrol ("label1") as label). text;
If (request. querystring ["ID"] = "dy ")
{
(MASTER. findcontrol ("image1") as image). imageurl = "~ /Images/ml0069.jpg ";
}
}

2. Use strong references

<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true "codefile =" default2.aspx. cs "inherits =" default2 "Title =" untitled page "%>
<% @ Mastertype virtualpath = "~ /Masterpage. Master "%>

You can then define public attributes or methods on the master page.
Public String GetUserName ()
{
Return page. User. Identity. Name;
}
Call
Label1.text = "welcome" + master. GetUserName ();

For Dynamic Master loading ~ The ASP. net2.0 Development Guide of instructor haogang is introduced ~ There are alsoCode~ But I personally think it is not convenient ~ So I didn't post it ~ Simple changes can be implemented through the above method

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.