问题
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;
- User Control init
- Master Page init
Content page init
LOAd Content page Load
- LOAd Master Page Load
LOAd User Control Load
Content page Render
- Master Page Pre Render
- 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