Where should stuff be done in an ASP.NET page?
Asked Answered
E

6

7

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.

Expound answered 27/7, 2009 at 19:9 Comment(2)
The ASP.Net page lifecycle claims another victim.Centerboard
What? Am I now doomed to become addicted or something? <g/>Expound
H
2

ASP.net page lifecycle

Hillard answered 27/7, 2009 at 19:10 Comment(0)
S
5

The first thing you need to learn to be able to understand the the questions you just asked is: PAGE LIFE CYCLE. It is a bitch sometimes, especially the ViewState part.

•What processing should I do in Page_Load?

•What processing should I do with the Load event? = Page_load

•What can I do in Page_Unload? Clean up

•What order do things get done in? PAGE LIFE CYCLE

•When is each event fired? PAGE LIFE CYCLE

•What is the page life-cycle? alt text

Edit: Image source: http://www.eggheadcafe.com/articles/20051227.asp

More info: http://www.codeproject.com/KB/aspnet/PageLifeCycle.aspx

Shutdown answered 27/7, 2009 at 19:12 Comment(4)
Do you have link to where that image came from? It's not going to be much good without some explanation.Expound
Not as much of a bitch as wiring up event handlers to dynamically created objects ;)Ushas
Unfortunately, that link doesn't provide much context.Expound
see the link in runtime's answerShutdown
B
5

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

Basipetal answered 27/7, 2009 at 19:31 Comment(0)
P
4

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.

Piero answered 27/7, 2009 at 19:11 Comment(1)
Both of those links look useful. Thanks.Expound
H
2

ASP.net page lifecycle

Hillard answered 27/7, 2009 at 19:10 Comment(0)
L
2

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.

Laux answered 27/7, 2009 at 19:51 Comment(1)
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)Expound
S
-1

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.

Subheading answered 27/7, 2009 at 20:12 Comment(2)
To the person who voted me down - it was meant to be a joke, silly!Subheading
And as a joke it shouldn't be near the top of the answers.Expound

© 2022 - 2024 — McMap. All rights reserved.