OnLoad vs Page_Load vs .Load delegate

Excellent article on this here: http://weblogs.asp.net/infinitiesloop/archive/2008/03/24/onload-vs-page-load-vs-load-event.aspx

Looked this up because I always wondered when and if you should call the base.OnLoad (or whatever page event it is) when you override these.

Which of course leads to the question should you override, attach to the event or just use the AutoEventWireup feature.

First of all the OnLoad even is responsible for firing the Load event (and still ditto any other page event) – So just to be clear: if you do not call base.On[Event](e); then the corresponding event will not fire! However this actually gives you a lot of control as you are able to do things before/after the event – which is useful for you standard PageBase.cs (i.e. check we have all required environment data and then fire the relevant page event).

Secondly I like the author’s general take on this – obviously all three options are viable and will do the job – so really it comes down to what you prefer.

Ive never personally liked the AutoEvent black box magic, where markup in the frontend determines what happens in the code behind – however this does come straight out of the box and saves you having to maintain a load of += delegate statements in you OnInit event. Also – even though it might not be pretty –  it’s a very well known convention that you will find the vast majority of .NET web devs are using,  so you will probably just inconvenience  yourself and others (spare a thought for the poor bugger who is going to inherit your code some day) if you start getting all purist on this.

My recommended solution – let VS generate it’s lovely little AutoEvent tags and markup and then use override onload in your base page/controls to gain some finegrained control on what happens when.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: