简体   繁体   中英

How do I get access to master page properties via user control markup?

I have been searching the internet and most I find resolves the issue of accessing the master page properties from the user control's code behind. But I am unable to find a solution where the user control can have access to the master page's properties within the markup.

Background:

The master page dynamically adds user control onto the page. The master page has two properties which the user control needs to access via markup.

Here is some code to represent my problem:

Master page's code behind properties:

public IModule Module
    {
        get
        {
            return MyContext.Current.Module;
        }
    }

    public IDictionary<string, object> Arguments
    {
        get
        {
            return MyContext.Current.Arguments;
        }
    }

Master page dynamically adds to control in code behind (it HAS to be dynamically added in master page's code behind):

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!(Page is VehicleForm) && !(Page is VsrManageForm) && !(Page is VpbManageForm))
            {
                MenuTab view = (MenuTab)this.LoadView(plhMenu, "~/Controls/MenuTab.ascx", "", MyContext.Current.Module);
            }
        }

User control's markup:

<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink1" runat="server" contextmodule='<%# Module %>' flowcall="FavouritesView" title="" rel="nofollow" ClientIDMode="Omitted">Shortlist</web:FlowLink>
<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink2" runat="server" contextmodule='<%# Module %>' flowcall="CompareView" title="" rel="nofollow" ClientIDMode="Omitted">Compare</web:FlowLink>
<web:FlowLink class="tools__lnk" arguments="<%# Arguments %>" id="flowLink5" runat="server" contextmodule='<%# Module %>' flowcall="UserView" title="" rel="nofollow" ClientIDMode="Omitted">Account</web:FlowLink>

Error:

Compiler Error Message: CS0103: The name 'Arguments' does not exist in the current context

Question: How do I access <%# Arguments %> and <%# Module %> master page properties from the user control?

It might be possbile (have not tested it though) to do something like this:

arguments="<%# ((MasterPageType)this.Page.Master).Arguments %>"

Although it does not look right. You might want to redesign the way you control gets the data. Or atthe very least do the same somewhere in code behind and verify whether a current masterpage is of an expected type.

Update. The final solution that OP used incorporated ideas above, and resulted in having properties like below declared in the control:

public IDictionary<string, object> Arguments
{
    get
    {
        MasterPageType master = this.Page.Master as MasterPageType;
        if (master != null)
        {
            return master.Arguments;
        }
        else
        {
            return null;
        }
    }
}

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