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

你离开我真会死。 提交于 2020-01-02 04:32:28

问题


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?


回答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




回答2:


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.




回答3:


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




回答4:


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>


来源:https://stackoverflow.com/questions/20758963/why-master-page-doesnt-have-preinit-event-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!