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

邮差的信 提交于 2019-12-05 09:23:35

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

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.

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

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