I am able to hook the event and also allowed to override load and init.., events in my aspx codebehind file. I have following questions.
public partial class Default : System.Web.UI.Page
{
public Default()
{
this.Init += Default_Init;
this.Load+=Default_Load;
}
protected void Default_Load(object sender, EventArgs e)
{ }
protected void Default_Init(object sender, EventArgs e)
{ }
protected override void OnLoad(EventArgs e)
{ }
protected override void OnInit(EventArgs e)
{ }
}
Qs:
- Why such option(both virtual method and event handler) is exist in Page class?
- When override methods got executed, my event handlers(Default_Load and Default_Init) are not executed. what is the reason behind?
- Should i call base.OnLoad(e); in overridden OnLoad method? Why should/shouldn’t I?
By default,
OnInitandOnLoadare not handlers ofInitandLoadevents. They are used to FireInitandLoadevents.When you override, you change the behavior of these methods thus, events are not fired by
OnInitandOnLoadmethods (unless you explicitly writebase.OnInitetc.)If you change the behavior of OnLoad and do not call base method, then you may break the page lifecycle of asp.net application. For instance,
Loadevent will not be called.