0

我的设置需要 2 级母版页,因为我在 Master Master 中加载数据,这些数据在我的应用程序中与不同的嵌套母版共享。

所以现在我需要Master Master先加载我的数据,然后在Nested Master中加载东西,然后在Page中加载东西。

当我只有一级大师时,我将加载顺序设置为:

  1. 嵌套主控 - 初始化
  2. 页面 - 加载

既然我多了一个Master等级,我该如何按以下顺序加载呢?

  1. 大师大师——?
  2. 嵌套大师 - ?
  3. 页 - ?

这是一个问题,因为 ASP.NET 出于某种原因首先加载了最内层。因此,假设提供相同的函数,ASP.NET 将按照 Page->Nested->Master 的顺序调用,而不是有意义的:Master->Nested->Page。在我个人看来,这完全违背了拥有母版页系统的目的。

4

1 回答 1

1

简短的回答是 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);
于 2012-06-14T16:36:38.290 回答