0

My setup require 2 level of master page because I am loading data in Master Master which is shared across my application with different Nested Masters.

So right now I need Master Master to load my data first, then load stuff in Nested Master, then load stuff in Page.

When I had just one level of master, I setup my load order as so:

  1. Nested Master - Init
  2. Page - Load

Now that I have an extra level of Master, how do I load in the following order?

  1. Master Master - ?
  2. Nested Master - ?
  3. Page - ?

This is a problem because ASP.NET for some reason load the inner most level first. So let's say giving the same function, ASP.NET will call in the order of Page->Nested->Master instead of what would make sense: Master->Nested->Page. Which in my personal opinion completely defeat the purpose of having a master page system.

Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174

1 Answers1

1

Short answer is PreRender, however sounds like you could benefit from moving some logic our of your master pages and into business objects/classes? Having different master pages depending on each other is probably not the best idea. If you need data to be available globally - load it in a business class, and cache it once created for however long is suitable (if just for the request use HttpContext.Items).

If you do need to stick with that setup, you also have the option of calling up through the masterpage hierarchy - so your root master (top level) can make options/data available OnInit. Anything else that needs this can then call - here's a method that loops all the masterpages in any given pages hierarchy and return first instance of required type:

/// <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;
}

To use:

Helpers.GetMasterPageOfType<GlobalMaster>(this.Master);
user369142
  • 2,575
  • 1
  • 21
  • 9