In C #, in the content page, get the variables in its template page, or the value

Source: Internet
Author: User

In CSDN's blog post saw the Muziduoxi article: http://blog.csdn.net/muziduoxi/article/details/5386543 Although the method mentioned inside did not solve my problem, But I also think of the method according to the methods used in the present, so I still need to thank because I want to initialize the variable is empty, if the use of Muziduoxi method can only read the value of the initialization time, I did a trace, When a page that has a template page is run, it is the definition of the variable that runs this page → the variable definition of the template page → The Page_Load method → to the pageload method of the template page, so if the template page variable I want to get is initialized in its Page_Load, So in this page Page_Load is not read, if you want to get to the method is: on this page in ASPX in the use of: <%= (this. Master asagency_web_agency_master). Systemurl%> and as for why Agency_web_agency_master This I understand is that it put the dot. In fact, this name will be after you enter as And then a space to prompt you.
The specific code is as follows:
The template page has a common variable definition: public string systemurl = "";
It is then used directly in the ASPX code of the content page: <%= (this. Master as Agency_web_agency_master). Systemurl%> can be output normally.
And not only can you get the field, you can also get an object if the template page has a class: public model.gy_agencyinfo modagency = new Model.gy_agencyinfo (); Class has a field that is: Systemurl
Used directly in the ASPX code of the content page: <%= (this. Master as Agency_web_agency_master). Modagency.systemurl%> can be output normally.
In order to conveniently remember the method of Muziduoxi, specially paste it to me here, to facilitate their own view:
Get the variables and property values for the Master page: template page:

[CSharp]View Plaincopyprint?
  1. Public partial class MasterPage:System.Web.UI.MasterPage
  2. {
  3. public int UserId {
  4. get { return 342;}
  5. }
  6. public String UserName = "SHENJK";
  7. protected void Page_Load (object sender, EventArgs e)
  8. {
  9. }
  10. }
Public partial class masterpage:system.web.ui.masterpage{public    int UserId {        get {return 342;}    }    public string UserName = "SHENJK";    protected void Page_Load (object sender, EventArgs e)    {    }}

Get the value on the content page:

[CSharp]View Plaincopyprint?
  1. protected void Page_Load (object sender, EventArgs e)
  2. {
  3. PropertyInfo PL = This . Master.gettype ().  GetProperty ("UserId");
  4. object o = pl. GetValue (This. Master, null); //o=342
  5. FieldInfo f = This . Master.gettype ().  GetField ("UserName");
  6. Object O1 = F.getvalue (This.  Master); //O1=SHENJK
  7. }
protected void Page_Load (object sender, EventArgs e)    {        PropertyInfo pl = this. Master.gettype (). GetProperty ("UserId");        Object o = pl. GetValue (this. Master, NULL); o=342                FieldInfo f = this. Master.gettype (). GetField ("UserName");        Object O1 = F.getvalue (this. Master);  O1=SHENJK           }

How to receive the value of a content page (the feeling should be a template page) control when referencing a template page

TextBox Myemail = (textbox) this. Page.Master.FindControl ("show").  FindControl ("email"); string email = myemail. Text;

This 2 lines of code let me find 2 days, I always think is:

TextBox Myemail = (textbox) this.  Page.findcontrol ("email"); string email = myemail. Text;

Always an error: Object references are not set to an instance of an object.

How to get the variables in the masterpage associated with the current page

For example: 1. a.aspx used a master page called Masterpage.master. MasterPage.master.cs defines an int variable flag = 1 How do I read the value of a flag variable in A.aspx.cs? Add the following code to the Page_Load event: ((MasterPage) Pa

For example: 1. a.aspx used a master page called Masterpage.master. MasterPage.master.cs defines an int variable flag = 1 How do I read the value of a flag variable in A.aspx.cs?

Add the following code to the Page_Load event:

((MasterPage) page.master). flag = 2; or int c= ((MasterPage) page.master). Flag;

can also (this. Master as MasterPage). Flag = 2

2. The following code example demonstrates how to use a content page to access the public properties SiteName on the master page in the previous code example.

     <%@   page   language= "C #"    masterpagefile= "~/ Masterpage.master "   title=" masterpage   Example "  %>            <script   runat= "Server" >           protected   void   Page_Load (object   sender,   EventArgs    e)           {               masterexample   m   =   (masterexample) page.master;               MyLabel. text   =   m.sitename;         }      </script>           <asp:Content   id= "Content1"    contentplaceholderid= "ConteNtPlaceHolder1 "   runat=" Server ">               hello,   master   pages!               <asp:Label   runat= "Server"    text= "Label"    id= "MyLabel" > </asp:Label>      </asp:Content>  

3. Get and set the value of a control in master page master

((Controls_wuctopbar) Master.findcontrol ("WucTopBar1")). Strnaviganame = "Movie Channel";

4. This can be Zu

In the content page add: <%@ MasterType virtualpath= "~/masterpage33.master"%> protected void Page_Load in background code (object Sen     Der, EventArgs e) {master.labeltext = "time Now:" + System.DateTime.Now.ToShortTimeString (); }

In C #, in the content page, get the variables in its template page, or the value

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.