¿Existe alguna condición en la que se ejecute un MasterPage Page_PreInit ()?

Based on a configuration setting I'd like to direct users to a "This site is currently offline" page. The master page seems like the common place to do that. I originally used this code:

MyConfig config = new MyConfig();

protected void Page_PreInit(object sender, EventArgs e)
{
    if (config.RefuseRequests)
        Response.Redirect("Offline.aspx");
}

A break point here is never hit.

After some digging I came across http://msdn.microsoft.com/en-us/library/dct97kc3.aspx which seems to say a master Page_PreInit is never called. Am I reading that right?

The above code doesn't show an error in visual studio and it seems like a useful step in the load flow. Are there any conditions where a master page Page_PreInit would be executed?

preguntado el 09 de marzo de 12 a las 15:03

Untested, therefor only as comment: MasterPages no tengo un PreInit evento pero ContentPages. So you could let all your pages inherit from a "Master-ContentPage" and use it's PreInit evento. odetocode.com/code/450.aspx -

@Tim Given how quickly you said "MasterPages no tengo un PreInit" the answer the main question appears to be "no." Is this stated at MSDN or in a book (I obviously don't have) or somewhere? Not having a reliable reference on this is what's nagging me. -

Aquí hay una lista of the MasterPage's events and here's a nice list of the sequence the events occur. I assume that it has no PreInit since it must be compiled and merged into the ContentPage. If it would have a PreInit, the ContentPage's PreInit could change the assigned Master what would cause unpredictable behaviour because the Master's PreInit would occur before the Master would be changed. -

@Tim Those first two links go to the page I mentioned in the question. I was hoping somebody had a more direct reference that said "they don't do that." Your reasoning helps though. Thanks. With everything Visual Studio yells about it's disconcerting a master page PreInit() is just fine with it. -

1 Respuestas

While the information out there isn't as clearly stated as I'd like the practical answer does appear to be "no, a master Page_PreInit() will never be hit."

Respondido el 14 de junio de 12 a las 18:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.