簡體   English   中英

如何按Master Master-> Nested Master-> Page的順序加載

[英]How to load in order of Master Master->Nested Master->Page

我的設置需要2個級別的母版頁,因為我正在將母版中的數據加載到應用程序中,並與其他嵌套母版共享該數據。

因此,現在我需要Master Master首先加載我的數據,然后在Nested Master中加載內容,然后在Page中加載內容。

當我只有一個母版時,我將裝載順序設置為:

  1. 嵌套大師-初始化
  2. 頁面-加載

現在,我有了一個更高級別的Master,如何按以下順序加載?

  1. 大師大師-?
  2. 嵌套大師-?
  3. 頁-?

這是一個問題,因為ASP.NET出於某種原因會首先加載最內部的級別。 因此,假設提供相同的功能,ASP.NET將按Page-> Nested-> Master的順序而不是有意義的順序進行調用:Master-> Nested-> Page。 以我個人的觀點,這完全挫敗了擁有母版頁系統的目的。

簡短的答案是PreRender,但是聽起來您可以通過將母版頁的某些邏輯移入業務對象/類中而受益? 具有彼此依賴的不同母版頁可能不是最好的主意。 如果您需要數據在全球范圍內可用-將其加載到業務類中,並在創建后對其進行高速緩存(無論創建時間長短)都適用(如果僅用於請求,則使用HttpContext.Items)。

如果確實需要堅持該設置,那么您還可以選擇通過母版頁層次結構進行調用-這樣您的根母版(頂層)可以在OnInit上使選項/數據可用。 然后,任何其他需要調用的東西都可以調用-這是一個循環任何給定頁面層次結構中所有母版頁並返回所需類型的第一個實例的方法:

/// <summary>
/// Iterates the (potentially) nested masterpage structure, looking for the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="currentMaster">The current master.</param>
/// <returns>Masterpage cast to specified type or null if not found.</returns>
public static T GetMasterPageOfType<T>(MasterPage currentMaster) where T : MasterPage
{
    T typedRtn = null;
    while (currentMaster != null)
    {
        typedRtn = currentMaster as T;
        if (typedRtn != null)
        {
            return typedRtn; //End here
        }

        currentMaster = currentMaster.Master; //One level up for next iteration
    }

    return null;
}

使用方法:

Helpers.GetMasterPageOfType<GlobalMaster>(this.Master);

暫無
暫無

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

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