Page Execution Sequence

Knowing the order in which ASP.NET page methods are executed is important. I'm not going to go into great detail here because there are a number of methods that most ASP.NET developers never see. I'll just talk about several that you'll commonly encounter.

When you create an ASP.NET Web application, Visual Studio creates boilerplate code for the Web Forms. Of these methods, the OnInit() method is the first executed when a page is loaded. From this method, the InitializeComponent() and the base.OnInit() methods are called. The InitializeComponent() method sets the event-handler delegates for each component that has event-handler methods. The base.OnInit() method allows the base class to perform initialization of its own.

We need to talk about an important notion now. That is the concept of post backs. ASP.NET performs a lot of its magic by posting itself (including its form data) to the server for processing. The server processes information and then serves up the same page to the client. So, in other words, when you click a button, the system first sends an HTTP POST command to the server. The server receives this command and then simply resends the page after having performed any addition processing.

The page controls, such as TextFields and Labels, are preserved in a hidden HTML field called VIEWSTATE. When the page is POSTed to the server, the server picks up this hidden field and is able populate the controls from the data the field contains. Then, when the page is served back to the client, the controls contain the persisted data.

If the page is being received in response to a post back, you can find this out. A page property called IsPostBack is true when the invocation is in response to a post back; when the invocation is not in response to a post back, the property is false. The following code shows how to determine whether the page is being posted back from inside the Page.Load event handler:

 private void Page_Load(object sender, System.EventArgs.e) {   if( IsPostBack )   {      // You only get here for post backs.   } } 
 Private Sub Page_Load(ByVal sender as object, _   ByVal e as System.EventArgs.e) Handles MyBase.Load   If IsPostBack Then     ' You only get here for post backs.   End If End Sub 

After the OnInit() method, the Page_Load() method is called. In many cases, user-interface objects are populated in the Page_Load() method. These objects will persist their data between post backs. For this reason, you don't need to repopulate them for each page invocation because they might already be populated. Populating controls unnecessarily will create a load on the server that it doesn't need to bear.

The last thing that happens is that event handlers for any controls for which an event was fired will be invoked. This means that, contrary to what you might guess, your event handlers invoke the events after OnInit() and Page_Load(). We often have an intuitive sense that button events will happen before the post back, but that is the opposite of what really happens.

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175 © 2008-2017.
If you may any questions please contact us: