Reputation: 3381
what are the pros and cons of using a standard event handler or overriding the base class of an asp.net page? Are there any? I've seen both used to do the same thing.
protected void Page_PreInit(object sender, EventArgs e)
{
//Put your code here
}
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//Put your code here
}
Upvotes: 2
Views: 2777
Reputation: 64943
First of all, both aren't the same thing.
OnLoad, OnInit and so on are the methods which fires events. Its goal is encapsulating event firing so, if these're virtual methods, derived classes will have the ability of override them and do something before and/or after firing some event.
I wouldn't ask for pros and cons, but "when to use them", because both are different things.
When to use event firing methods
When to use events directly
Pay attention because if you tend to override event firing methods you're modifying the way events are fired themselves, which is a critical thing.
If you need to do something during some page or control life cycle, subscribe to the event, and if you need to subscribe to do something before some event is fired, implement a new event and fire it before next one gets raised:
public event EventHandler CustomEvent;
protected virtual void OnCustomEvent(EventArgs e)
{
if(CustomEvent != null)
{
CustomEvent(this, e);
}
}
protected override void OnPreRender(EventArgs e)
{
OnCustomEvent(new EventArgs());
base.OnPreRender(e);
}
In my opinion, overriding the way an event gets fired when the situation is some object needs to be notified when something happens is a bad usage of C# language, since this is achieved by using event delegation model.
Upvotes: 1
Reputation: 148554
Page_PreInit will be called if autoeventWireUp is On.
The func OnPreInit is virtual , and in your page is overrided.
but the master function which is virtual - is executing the code which triggers the Page_PreInit.
so you have to call base.OnPreInit(e); even if you override it.
IF YOU NEED TO PUT SOME CODE BEFORE OR AFTER THE OnPreInit so use the second one .
Upvotes: 0
Reputation: 17701
There is much difference in both of the name that you have mentioned, I would like to tell you about that, actually overriding means to use the same method with the same name in the child class, but only the parameters are different in both of the methods
, If suppose take the example of the calculating the area, and if you want to calculate the area of two to three different object, then you have to take three different names rather you can use the same name for each, just the different parameters.
Upvotes: 0
Reputation: 24312
if you use the override you can decide when the custom function should be execute. after or before base method. but if you use auto wireup events it will execute after the base event.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
//Put your code here
}
or
protected override void OnPreInit(EventArgs e)
{
//Put your code here
base.OnPreInit(e);
}
Upvotes: 1