Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 264
Aditya Singh Avatar asked Dec 24 '13 10:12

Aditya Singh


People also ask

In which event a Master page can be set dynamically?

Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage. Save this answer.

In which event the master page controls are loaded?

Master page controls PreRender event.

What happens in PreInit event of a page?

The event enables you to set values that are used later in the page life cycle. You can dynamically set a master page or a theme for the requested page, and create dynamic controls.

Which events can be used to programmatically set the master page file for an ASP.NET page?

To set the master page programmatically, then, we can either create an event handler for the PreInit event or override the OnPreInit method.


2 Answers

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.

like image 190
R.C Avatar answered Oct 24 '22 09:10

R.C


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

like image 1
Just code Avatar answered Oct 24 '22 11:10

Just code