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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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.

Method 4

ASP.net page lifecycle

Method 5

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.

Method 6

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x