|
Web Forms Page Life Cycle
In general, the life cycle for a Web Forms page is similar to that of any Web process
that runs on the server. Certain characteristics of Web processing information
is passed via HTTP protocol, the stateless nature of Web pages, and so on. However,
the ASP.NET page framework performs many Web application services for us. It is important
to understand the sequence of events that occur when a Web Forms page is processed.
The Life Cycle of a Web Forms Page
It will be helpful to understand some fundamental characteristics of how Web Forms
pages work in Web applications before we examine the details of what goes on
inside a page when it is processed.
Round Trips
Most Web pages require processing on the server. For example, consider a products
page used to check the availability of a certain product. When a user selects his
product and hits the submit button the page must check on the server to see whether
the selected product is available or not. This kind of functionality is achieved by
handling server control events. Whenever a user interaction requires processing on
the server, the Web page is posted back to the server, processed and is returned back
to the browser. This sequence is called round trip. The image below demonstrates server
round trip.
In any Web scenario, Web pages are recreated with every round trip. When the
server finishes processing and sends the page to the browser, it discards the page
information. This frees server resources after each request and a Web application
can scale to support hundreds or thousands of simultaneous users. The next time the
page is posted, the server starts over in creating and processing it, and for this
reason, Web pages are said to be stateless. Stateless means the values of a page's
variables and controls are not saved on the server.
ASP.NET works around the above said limitations in the following ways:
-
ASP.NET saves page and control properties between round trips. This is referred
to as saving the view state of the control.
-
It provides state management facilities so that you can save your own variable and
application-specific or session-specific information between round trips.
-
It can detect when a form is requested for the first time versus when the form is
posted, and allows you to program accordingly. You may want a different behavior
during a page postback versus an initial request.
Stages in Web Forms Processing
| Stage | Means | Use |
| Page Initialization | The page's Page_Init event is raised, and the page and control view state are restored. | During this event, the ASP.NET page framework restores the control properties and postback data. |
| User Code Initialization | The page's Page_Load event is raised. | Read and restore values stored previously,
Using the Page.IsPostBack property, check whether this is the first time the page is being processed.
If this is the first time the page is being processed then perform initial data binding.
Otherwise, restore control values.
Read and update control properties. |
| Validation | The Validate method of any validator Web server controls is invoked to perform the control's specified validation. | Test the outcome of validation in an event handler |
| Event Handling | If the page was called in response to a form event, the corresponding event handler in the page is called during this stage | Perform application-specific processing and handle the specific event raised. |
| Cleanup | The Page_Unload event is called because the page has finished rendering and is ready to be discarded. | Perform final cleanup work. Close files, closing database connections and discard objects. |
|