Documente Academic
Documente Profesional
Documente Cultură
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.
I'll set the stage with a simple submission form written in ASP.NET with C#. The page is loaded
for the first time and has several server-side Web controls on it. When the Web server receives a
request for the page, it will process our Web controls and we will eventually get rendered HTML.
The first step in processing our page is object initialization.
1. Object Initialization
A page's controls (and the page itself) are first initialized in their raw form. By declaring your
objects within the constructor of your C# code-behind file (see Figure 1), the page knows what
types of objects and how many to create. Once you have declared your objects within your
constructor, you may then access them from any sub class, method, event, or property. However,
if any of your objects are controls specified within your ASPX file, at this point the controls have
no attributes or properties. It is dangerous to access them through code, as there is no guarantee
of what order the control instances will be created (if they are created at all). The initialization
event can be overridden using the OnInit method.
4. Object Load
Objects take true form during the Load event. All objects are first arranged in the page DOM
(called the Control Tree in ASP.NET) and can be referenced easily through code or relative position
(crawling the DOM). Objects are then free to retrieve the client-side properties set in the HTML,
such as width, value, or visibility. During Load, coded logic, such as arithmetic, setting control
properties programmatically, and using the StringBuilder to assemble a string for output, is also
executed. This stage is where the majority of work happens. The Load event can be overridden by
calling OnLoad as shown in Figure 3.
8. ViewState Saved
The viewstate is saved after all changes to the page objects have occurred. Object state data is
persisted in the hidden <input> object and this is also where object state data is prepared to be
rendered to HTML. At the SaveViewState event, values can be saved to the ViewState object, but
changes to page controls are not. You can override this step by using SaveViewState, as shown in
Figure 5.
Figure 5: - Values are set for controls in OnPreRender. During the SaveViewState event, values
are set for the ViewState object.
9. Render to HTML
The Render event commences the building of the page by assembling the HTML for output to the
browser. During the Render event, the page calls on the objects to render themselves into HTML.
The page then collects the HTML for delivery. When the Render event is overridden, the developer
can write custom HTML to the browser that nullifies all the HTML the page has created thus far.
The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML
to be streamed to the browser. Changes can still be made at this point, but they are reflected to
the client only. The Render event can be overridden, as shown in Figure 6 (below).
10. Disposal
After the page's HTML is rendered, the objects are disposed of. During the Dispose event, you
should destroy any objects or references you have created in building the page. At this point, all
processing has occurred and it is safe to dispose of any remaining objects, including the Page
object. You can override Dispose, as shown in Figure 6.
Figure 6 - The Render event will output custom HTML to the browser through the HtmlTextWriter
object.
Conclusion
Each time we request an ASP.NET page, we run through the same process from initialization to
disposal. By understanding the inner workings of the ASP.NET page process, writing and
debugging our code will be much easier and effective (not to mention less frustrating).