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