I’ve only been a "developer" for about two years now.  I put the term in quotations, because I don’t really count college work as development and my first job out of college was basically to occupy space.  With that being said, there are two main reasons that I got into this field: 1) I love to learn (and this field offers a never-ending supply of new material) and 2) I love to build things.  My quest to fulfill goal number one has often resulted in better results out of goal number two.

 

It wasn’t long after I was hired at my current job that I realized the wealth of information that was being disseminated through blogs, and one of my favorite bloggers has got to be Scott Hanselman.  Recently I came across some ASP.NET interview questions on Scott’s blog.  The post is a little dated (2004), but most of the questions are still relevant.  At least, they’re relevant enough that I feel bad for not being able to answer most of them.  Since I’m always up for a challenge, I thought it’d be a great learning experience to devote a series of blog entries to answering these questions.  This post shall contain my attempt at answering the first:

 

Disclaimer: I know Einstein said "The secret to creativity is knowing how to hide your sources.", but I should point out that a lot of the information contained from this point on was gathered from these two articles on MSDN: ASP.NET Page Life Cycle Overview and Page Events.

 

Question

  • From constructor to destructor (taking into consideration Dispose() and the concept of non-deterministic finalization), what events are fired as part of the ASP.NET System.Web.UI.Page lifecycle. Why are they important? What interesting things can you do at each?

 

Answer

  • PreInit Event
    • Called at the very beginning of page initialization, this event has several practical uses in your code behind pages.  A few of these uses are: Checking the IsPostBack property, dynamically setting a master page or theme property, dynamically creating controls, and reading or setting property values.  If you were so inclined, you could set a master page dynamically in the PreInit event as such:
  • Init Event
    • Called after the PreInit event and after all controls have been initialized and any skin settings have been applied, this event is very useful for initializing control properties before the page loads.
  • InitComplete Event
    • Like the name implies, this event is called after the Init event is completed.  I’m at a bit of a loss for thinking up useful and/or interesting things to do with this event, but if there were any tasks that needed to be completed once the controls were initialized, this would be the place to do it.
  • PreLoad Event
    • This event is called immediately before the Page.Load event and after all postback data has been processed.
  • Load Event
    • Probably the most familiar since it comes for free whenever you switch to the code-behind (or hit F7) on an aspx page in Visual Studio.  This event is useful for setting control properties and establishing database connections (both of which could be used to populate a drop-down from the database, for example).
  • LoadComplete Event
    • What else could possibly follow up the Load event?  This event is useful for doing any processing that requires that all controls on the page first be loaded.
  • PreRender Event
    • This event is useful for making any final changes to the page or the controls before the output is rendered to the browser.
  • PreRenderComplete Event
    • Called after PreRender is completed.  This is the last event to be called before the viewstate is saved.
  • SaveStateComplete Event
    • Called once the viewstate has been saved.  Use this for any processing that requires the viewstate to be saved but that doesn’t affect the rendering of the controls.
  • Render
    • While not technically an event, this method gets called for every control on the page.  When creating a custom control (that requires custom markup), you could override this method to provide your own markup.
  • Unload Event
    • Use this event to do any final cleanup work on the page.  Any controls on the page will also have an unload event that can be used to do cleanup on the controls themselves.
  • Disposed Event
    • This is the last event called for a control when it is released from memory on the server.  The is the last stage of the ASP.NET lifecycle when a page is requested.

 

I’m not sure that I’ve technically answered the question as thoroughly as one might need to in an interview situation, but I think I’ve provided enough information that the aspiring programmer could think of other uses for each of these events.  If you have any additional comments that others might find useful (or if I’ve missed an event or incorrectly described one), please don’t hesitate to leave them in the comments.