繁体   English   中英

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

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

我一直在搜索Internet,大多数发现可以从后面的用户控件代码中解决访问母版页属性的问题。 但是我无法找到一种解决方案,使用户控件可以访问标记内的母版页属性。

背景:

母版页将用户控件动态添加到页面上。 母版页具有用户控件需要通过标记访问的两个属性。

这是一些代码来代表我的问题:

属性后面的母版页代码:

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

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

母版页在控件中动态添加代码(它必须在母版代码中动态添加):

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);
            }
        }

用户控件的标记:

<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>

错误:

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

问题:如何从用户控件访问<%#参数%>和<%#模块%>母版页属性?

做这样的事情可能是可能的(虽然没有测试过):

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

虽然看起来不正确。 您可能需要重新设计控制获取数据的方式。 或者至少在后面的代码中的某处进行相同的操作,并验证当前母版页是否属于预期类型。

更新。 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