简体   繁体   English

如何通过用户控件标记访问母版页属性?

[英]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. 我一直在搜索Internet,大多数发现可以从后面的用户控件代码中解决访问母版页属性的问题。 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: OP使用的最终解决方案结合了上面的想法,并导致控件中声明了如下所示的属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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