tags:

views:

1942

answers:

5

Between handling the Page_Init event or overriding the OnInit method of a Page, which one is better to use? Thanks.

+1  A: 

There is a good answer here http://objectmix.com/dotnet/251824-oninit-page_init.html.

http://www.justskins.com/forums/page_init-and-page_load-59045.html has a more indepth answer.

If AutoEventWireup is set to false you need to override OnInit as the Page_Init isn't automatically available.

apocalypse9
+4  A: 

Overriding the base type's method is preferable as a virtual call is simpler and cleaner than creating a delegate attaching an event to a method group.

Also, relying on AutoEventWireup being set to true means that you are introducing overhead into the parsing of your page code as ASP.NET will have to create any delegates for you at execution-time.

Andrew Hare
Plus you get to choose if and when to call the base method, too - can't do that with the Page_Load style
marc_s
@marc_s - That is an _excellent_ point as well.
Andrew Hare
+5  A: 

I had this very question about a year ago, I settled on overridding as opposed to the On_X Events. Here is the article I read covering the pros and cons: http://weblogs.asp.net/infinitiesloop/archive/2008/03/24/onload-vs-page-load-vs-load-event.aspx

Nick Riggs
Thanks Nick! That was a good article. I was looking for something like that. Greatly appreciate it. :=)
ban-G
+2  A: 

Hi, Basically there is no difference in this two appoaches. That's what is done in OnInit in Page class:

protected internal override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (this._theme != null)
    {
        this._theme.SetStyleSheet();
    }
    if (this._styleSheet != null)
    {
        this._styleSheet.SetStyleSheet();
    }
}

If we will open base.OnInit we will se that that is the place where Page_Init is fired:

protected internal virtual void OnInit(EventArgs e)
{
    if (this.HasEvents())
    {
        EventHandler handler = this._occasionalFields.Events[EventInit] as EventHandler;
        if (handler != null)
        {
            handler(this, e);
        }
    }
}

So basically there is no difference in this two approaches. However you need to call base.OnInit in your overriden method if you will choose to use override instead of event. And another difference is that if you are using override you can run some code just after Theme is applied.

Regards.

P.S. The only thing I recommend is to use the same approach all over the application.

Dmitry Estenkov
Yes there is - only when overriding the OnXXX method do you have the choice of WHEN to call the base method (or even whether or not to call it at all). This might seems a bit nit-picky, but in certain cases, this can be crucial. Therefore my advice: always override!
marc_s
And i've mentioned that ability - "And another difference is that if you are using override you can run some code just after Theme is applied."
Dmitry Estenkov
A: 

I find knowing the lifecycle of when events are fired helpful in figuring out where you should put your code.

Gord