In what order do pages and events load in ASP.NET pages ?


I’ve been working on two different web applications lately and I’ve been working with web user controls to handle searching so that I can reuse the search capability on multiple pages.

I have the user control show a Gridview with search results and allow the user to select a row to edit.  At this point I store the selected ID of the chosen row in session and have my container page grab the session value and populate a second table with details of the selected record to edit or what have you.

Seems simple enough right ?  Except for the fact that it sometimes wont work depending on how you build your page.  If you use a control to display your details for your selected record chances are it will work ok if it is connected to a DataSource to retrieve the data.

However if you build your own form with just a HTML table and some Textboxes on the page and expect to fill them in you will run into problems.  Most likely what will happen is you will select a record and nothing will happen on the first click.  After that clicking a second record will display the first record.  Clicking a third record will display the results from the second record etc etc.

Whats happening ? well your experiencing a problem with the order that the page is loaded and events fire.

Lets setup a common example:

  • A Master page which contains
    • An ASPX web form page which contains
      • A Web User Control inside the page which contains
        • A button to fire some code in a button_click event

Now lets say you click that button.  You would assume that the button code would run and then the page would post back and the master page code, page load code etc would run.  What really happens is much different.

Here are a few links to documentation on the page load life cycle.

What you will see from the first link provided is that the order is fairly complex.  What concerns us for this example is the Load event, control events, and PreRender event.

With a control like a Gridview, details view,or form view that is connected to a DataSource control you should be ok.  The button code will run first storing the session value and then the PreRender will fire which calls the databind() method for any DataSource controls. This means that the DataSource will grab the session value and update the view correctly.

The other method of trying to manually fill in some Textboxes with textbox1.text = “text” based off data you have in a DataTable from a TableAdapter that runs in the page_load for example will operate differently.  The Load methods are being called and retrieving your data and THEN the control button code is run storing the new data ID in session.

A simple example you can do to test what happens when is to make a page inside your master page and then create a web user control and put it in the new page and follow these steps.

  • Inside the code behind .cs pages add a Trace.Warn(“inside page_load”); line to each Page_load event.
  • Add the Page_Init and Page_PreRender events as well and add similar trace warning messages.
  • Turn on tracing in the ASPX page by adding Trace=”true” to the page directive (the first line of code in your aspx page)
<%@ Page Title=”” Language=”C#” MasterPageFile=”~/myMaster.master” AutoEventWireup=”true” CodeFile=”lifecycle.aspx.cs” Inherits=”_lifecycle” trace=”true”%>

You should end up with tracing information when you load up the page something like below. Begin Init
–> Inside user control Page_Init
–> Inside master page Page_Init
–> Inside lifecycle page Page_Init End Init Begin InitComplete End InitComplete Begin PreLoad End PreLoad Begin Load
–> Inside lifecycle page Page_Load
–> Inside master page Page_Load
–> Inside user control Page_Load End Load Begin LoadComplete End LoadComplete Begin PreRender
–> Inside lifecycle Page_PreRender
–> Inside master page Page_PreRender
–> Inside user control Page_PreRender End PreRender

Hopefully this helps you sort out your page loading problems !