Why master page doesn't have PreInit event in ASP.NET?
Asked Answered
U

4

8

The following is the sequence in which events occur when a master page is merged with a content page:

Content page PreInit event.
Master page controls Init event.
Content controls Init event.
Master page Init event.
Content page Init event.
Content page Load event.
Master page Load event.
Master page controls Load event.
Content page controls Load event.
Content page PreRender event.
Master page PreRender event.
Master page controls PreRender event.
Content page controls PreRender event.
Master page controls Unload event.
Content page controls Unload event.
Master page Unload event.
Content page Unload event.

But why master page doesn't have a PreInit event in ASP.NET?

Upsetting answered 24/12, 2013 at 10:10 Comment(1)
what is Master page and Content page : controls Init event, Load event etc. defines. Can we override that events ?Alexandraalexandre
B
7

Master pages inherits:System.Web.UI.MasterPage and as per the design of this MasterPage class no such PreInit event is defined for this class.

Master pages are derived from Control class as seen in below hierarchy:

System.Object
  System.Web.UI.Control
    System.Web.UI.TemplateControl
      System.Web.UI.UserControl
        System.Web.UI.MasterPage

Therefore as can be guessed now, Master pages behave and in essence are treated like a control and have events similar to other asp.net server controls.

One suggested reading is this.

Birthroot answered 24/12, 2013 at 12:16 Comment(1)
I honestly think this comes closest to answering the actual question of WHY a master page doesn't have PreInitWattle
F
1

Masterpage doesn't have PreInit method.

There are several alternatives you can adopt.

1, Create a common base page class for all other pages to inherit, set the theme property in that class; http://www.odetocode.com/Articles/450.aspx

Fungus answered 24/12, 2013 at 10:12 Comment(0)
F
0

Sequnce of the event will be like below;

  1. User Control init
  2. Master Page init
  3. Content page init

  4. LOAd Content page Load

  5. LOAd Master Page Load
  6. LOAd User Control Load

  7. Content page Render

  8. Master Page Pre Render
  9. User Control Render

For more details with example please see the below link;

http://getmscode.blogspot.in/2014/11/sequence-of-events-in-master-page-and.html

Felicidad answered 10/11, 2014 at 4:22 Comment(0)
F
0

Same as UrlMapping model, create a class that should be generated by IHttpModule, then add its referance to Web.config

public class MasterPageModule: IHttpModule
{    
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
 //your code
}
}


<httpModules>
   <addname="MasterPageModule"type="MasterPageModule"/>
</httpModules>
Fourier answered 5/3, 2019 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.