簡體   English   中英

母版頁需要知道當前內容頁是否未使用ContentPlaceHolder

[英]Master page needs to know if a ContentPlaceHolder hasn't been used by current content page

我有一個母版頁,其中包含幾個ContentPlaceHolders。 並非所有內容都一直被當前內容頁面使用。 在頁面渲染期間,當當前內容頁面未使用ContentPlaceHolder時,母版頁面需要設置一個屬性。 意味着內容頁面可能未引用ContentPlaceHolder。

母版頁遍歷其ContentPlaceHolders並找出當前內容頁面尚未使用的內容的最佳方法是什么? 尋找一種解決方案,該解決方案不涉及從內容頁面到母版頁面的任何通信。

在MasterPage的PreRender事件中執行此操作-到頁面周期為止,此時將創建所有控件。

YourMasterPage.master.cs

protected void Page_PreRender(...) {
    HidePlaceholders(this);
}

protected void HidePlaceholders(Control control)
{
     foreach (Control ctrl in control.Controls)
     {
         if (ctrl is ContentPlaceHolder)
         {
            if (ctrl.Controls.Count == 0)
            {
                ctrl.Visible = false;
            }

         }
         else
         {
            if (ctrl.Controls.Count > 0)
            {
                HidePlaceholders(ctrl);
            }
         }

     }
}

是否有理由不在占位符中使用默認內容? 像這樣:

<!-- Site.Master -->
<asp:ContentPlaceHolder ID="SomeCotnent">
    <p>Content here will only appear if it's not overridden in content pages</p>
</asp:ContentPlaceHolder>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM