CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Karl Seguin

developer @ Fuel Industries ottawa, ontario

Page_Load is NOT evil

There's a post over at SitePoint calling Page_Load evil. The complaint is pretty much that because it's automatically there (VS.NET generates the Page_Load handler by default) people just cram everything in there.

Technically some broad statements about Page_Load are made which I don't agree with. Since when is Page_Load too late to add controls? Page_Load is too early to deal with button clicks? You can only have 1 event at a time, it's either going to be before or after...events are fired AFTER Page_Load so that you can dynamically re-add controls and have their event raised.

Overall, I agree that a lot of people abuse Page_Load. They never understand why using PreRender, or Init might be much better alternatives given a particular case. ASP.NET is hard - I've even argued that it's too hard.

In this case though, the blame squarely goes to the overwhelming amount of bad programmers. If you're doing ASP.NET you simply MUST understand the page lifecycle, even though it's relatively super-complex. Take 1 day, load up Reflector, examine the Page class (specifically the ProcessMain function), load up some tests, play with viewstate, dymamic controls, events and prerender and become a better programmer. You're either an ASP.NET programmer or you aren't.

The article proposes a more comprehensive alternative to just Page_Load. All he's really doing is renaming events, Init to AddDynamicControls, PreInit to ChooseMasterPage, Load to LoadData and PreRender to BindData. Those might be good guidelines for someone just starting to learn ASP.NET, but it doesn't really do anything to promote an understanding of and proper use of the page lifecycle.


Published Jan 23 2007, 09:16 AM by karl
Filed under:

Comments

Jason Bock said:

I find it evil, but for a completely different reason. It should override OnLoad(), not handle the Load event. You're subclassing Page anyway; handling an event from....yourself seems really weird. I always nuke this and do the "override OnLoad()" dance.

BTW see this: http://odetocode.com/Blogs/scott/archive/2005/10/05/2313.aspx, and this: http://weblogs.asp.net/jgonzalez/archive/2004/08/25/220709.aspx.

# January 23, 2007 10:14 AM

karl said:

Jason,

I normally do the same thing. I used override OnLoad in a post recently, and a couple people commented on it being weird...glad to know I'm not alone:

http://codebetter.com/blogs/karlseguin/archive/2006/12/01/How-to-hire-a-programmer-_2D00_-Part-2-_2D00_-Improve-this-code.aspx

# January 23, 2007 10:42 AM

kyle said:

I agree with the idea that bad programmers abusing page_load is the problem.  If your going to work with ASP.NET, being familiar with the page event life cycle is critical.  When you start to work with dynamically adding controls and other advanced topics it becomes even more important.  I keep a diagram of the events taped to the wall by my desk.  It is complicated, however gaining a basic understanding of the goings on in each even is not that tough.

# January 23, 2007 11:24 AM

Wyatt Barnett said:

W00t. I made codebetter!

The post was aimed at the more junior developers of the world. As for your comments:

1) Page_Load is generally too late to add controls because you need them around during  Init to hook properly into viewstate. Now, if you don't have to worry about postbacks, that is great. If you are trying to make some sort of dynamic form, then you might get some funny behavior.

2) One should really be dealing with a button click in a handler for said button click, not in a massive Page_Load method. Yes, you could technically handle the request in Page_Load, but that does not mean you should.

3) Examples were short. I should probably have expanded, but I usually have multiple handlers for the Load event, each handling its own piece of the pie. Makes for much more maintainable code, IMHO.

# January 23, 2007 1:20 PM

Fregas said:

Totally agree with your post.  There are good uses of Page_Load and bad uses, and I do think its kind of silly to handle and event when you could just override OnLoad, but i tend to do it the way the designer does it.

I really think that some porton of the blame is on Microsoft who made asp.net way too complex especially in the page/event lifecycle.  This makes new programmers run to the simplicity in things like Ruby On Rails or ugly old PHP.  I think i'd get a lot more productivity out of my programmers, especially juniors, using a simpler web framework.  Don't get me wrong...I love the .NET framework and there are things I love about ASP.NET (validation, viewstate, controls) but some of it DOES require a rocket science degree.

# January 23, 2007 2:47 PM

Jay Kimble said:

Something I did early on was print out this graphic from Ray's blog.  Since I'm betting none of you had seen it.  It really is helpful to the newbie (and the not-so newbie).

http://codebetter.com/blogs/raymond.lewallen/archive/2005/03/10/59583.aspx

# January 24, 2007 7:23 AM

Mark said:

Does anyone have recommendations for good articles describing the page lifecycle? I think the theme here is that better education is required to ensure newbie programmers understand what each part of the page lifecycle is intended for and in which circumstances each should be used.

# January 24, 2007 7:44 AM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 21, 2007 8:52 AM

David said:

This post makes a lot more sense than the reactionary crap it was written as a response to..

-David.

# March 4, 2008 5:58 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!