我的设置需要2级主页,因为我在Master Master中加载数据,该数据在我的应用程序中与不同的嵌套大师共享。
所以现在我需要Master Master首先加载我的数据,然后在Nested Master中加载东西,然后在Page中加载东西。
当我只有一个级别的主人时,我设置我的加载顺序如下:
既然我有一个额外的Master级别,我该如何按以下顺序加载?
这是一个问题,因为ASP.NET由于某种原因首先加载最内层。因此,假设提供相同的功能,ASP.NET将按Page-> Nested-> Master的顺序调用,而不是有意义的:Master-> Nested-> Page。在我个人看来,这完全违背了拥有母版页系统的目的。
答案 0 :(得分: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);