views:

346

answers:

6

I'm very new to ASP.NET and, after beating my head on a few problems, I'm wondering if I'm doing things wrong (I've got a bad habit of doing that). I'm interested in learning about how ASP.NET operates.

My question is: Where can I find documentation to guide me in deciding where to do what processing?

As a few specific examples (I'm interested in answers to these but I'd rather be pointed at a resource that gives more general answers):

  • What processing should I do in Page_Load?
  • What processing should I do with the Load event?
  • What can I do in Page_Unload?
  • What order do things get done in?
  • When is each event fired?
  • What is the page life-cycle?


edit: this question might also be of use to some people.

+2  A: 

ASP.net page lifecycle

AaronS
+4  A: 

Here are some good links to get you started. Understanding how the ASP.NET life-cycle fits together is critical to understanding how your code will interact with it.

ASP.NET Page Life Cycle Overview:

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend. Additionally, if you develop custom controls, you must be familiar with the page life cycle in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code. (The life cycle of a control is based on the page life cycle, but the page raises more events for a control than are available for an ASP.NET page alone.)

The ASP.NET Page Life Cycle:

When a page request is sent to the Web server, whether through a submission or location change, the page is run through a series of events during its creation and disposal. When we try to build ASP.NET pages and this execution cycle is not taken into account, we can cause a lot of headaches for ourselves. However, when used and manipulated correctly, a page's execution cycle can be an effective and powerful tool. Many developers are realizing that understanding what happens and when it happens is crucial to effectively writing ASP.NET pages or user controls. So let's examine in detail the ten events of an ASP.NET page, from creation to disposal. We will also see how to tap into these events to implant our own custom code.

Andrew Hare
Both of those links look useful. Thanks.
BCS
+5  A: 
Colin
Do you have link to where that image came from? It's not going to be much good without some explanation.
BCS
Not as much of a bitch as wiring up event handlers to dynamically created objects ;)
BenAlabaster
Unfortunately, that link doesn't provide much context.
BCS
see the link in runtime's answer
Colin
here's a more generalized overview: http://www.15seconds.com/issue/020102.htm
Colin
+4  A: 

The links posted by various folks are very helpful indeed - the ASP.NET page life cycle really isn't always easy to grok and master!

On nugget of advice - I would recommend preferring the overridden methods vs. the "magically" attached methods, e.g. prefer the

protected override void OnLoad(EventArgs e)

over the

protected void Page_Load(object sender, EventArgs e)

Why? Simple: in the overridden methods, you can specify yourself if and when the base method will be called:

protected override void OnLoad(EventArgs e)
{ 
    base.OnLoad(e);
    // your stuff
}

or:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    base.OnLoad(e);
}

or even:

protected override void OnLoad(EventArgs e)
{ 
    // some of your stuff
    base.OnLoad(e);
    // the rest of your stuff
}

or even:

protected override void OnLoad(EventArgs e)
{ 
    // your stuff
    // not call the base.OnLoad at all
}

You don't have that flexibility in the Page_Load() version.

Marc

marc_s
+2  A: 

I would definitely recommend you reading this:

http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

If you're new to asp.net you'll have some trouble getting all of that, but really, I have yet to find such a detailed document on the topic comming from ms documentation or any ms employee blog.

I did it the hard way and followed every path I could using disassembled code but that guy really took the time to write it.

That a cool read and worth the time. +1 However, it stops just below the point I'm interested in learning about (it's *to* low level)
BCS
Sorry BCS, you're absolutely right about that.
A: 

Basically try and do it in Page_Load and if that doesn't work, try it in either Page_Init or Page_Render. Normally one of them works :) That's the scientific approach.

Dan Diplo
To the person who voted me down - it was meant to be a joke, silly!
Dan Diplo
And as a joke it shouldn't be near the top of the answers.
BCS
OK, fair point!
Dan Diplo