简体   繁体   中英

Get access to user control from a nested master page

Is there a way to get access to a puplic property on a user control from a nested master page?

Let me explain further

I have 3 deep master pages

  1. global.master
  2. LargeTopNav.master (inherits global.master)
  3. LargeTopNav25-50-25.master (inherits LargeTopNav.master)
  4. feature.aspx - This page is where I would like to access a custom user control I have on LargeTopNav.master to be able to set a property.

I'm fairly new to .net so any help is appreciated.

There is a MasterType property you could set on the feature.aspx page like eg:

<%@ MasterType VirtualPath="~/masters/LargeTopNav.master" %>

Then, in turn, provide access to the control's property through a property you create in your LargeTopNav.master master page class:

public partial class LargeTopNavMaster : MasterPage
{
    // ...

    public string ThePropertyOfTheContainedControl
    {
        get { return MyContainedControl.TheProperty; }
        set { MyContainedControl.TheProperty = value; }
    }

    // ...
}

Last, in your feature.aspx page, access the property of the master page that provides access to the underlying control:

public partial class Feature : Page
{
    // ...

    protected void Page_Load( object sender, EventArgs e )
    {
        Master.ThePropertyOfTheContainedControl = "Some nice text.";
    }

    // ...
 }

Generally what I do in this scenario is make your master page implement an interface (which will have the property for your user control), and then from your page feature.aspx use this.Master (or this.Master.Master , make sure to check for null ) to get a reference to the master page.

Then, just typecast the master page to your interface, and access the property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM