Mastering the .NET Code model is an essential step towards
web development nirvana. The journey towards a complete understanding of how
.NET works within the web world can be a long one. In this article I will show
you how to shorten the learning curve, gain important insights about web
development, and provide access to critical learning resources.
First, let’s discuss our working environment. Whether you
are using code generator tools like Iron
Speed Designer or Visual Studio (from scratch) to build web applications,
you first need to grasp the concept of a “stateless page”.
This simply means that when you have a page (any page) displayed in your
browser, this page consists of nothing more than text and graphics, excepting
Windows or desktop programs work differently. There are
almost always processes running in the background, used to support the primary
function of that program, whether it is an operating system, word processor or
Pages in web applications or web sites (always viewed in a
browser) are “stateless”, because a long time ago internet speeds were very
slow. Consequently, you could not transfer large blocks of information at one
time, without very noticeable delays – to the point where applications were too
slow to use! And so developers would send blocks of text and graphics in small
chunks. If a change to a page was made, then the entire page would have to be
re-sent from the server to the client, and any changes would be lost along the
Since the early beginnings
of the Internet, developers have had to deal with this underlying fact.
Although internet speeds have risen significantly since, we still have the same
technology base to deal with.
Browser manufacturers got around this limitation by
introducing something called “ViewState”.
ViewState is a means of storing certain information locally on the client
computer, either within the browser memory itself, or via a cache file on the
This brings us to the next logical question: “How do I know
the difference between when a page has been loaded for the first time, and when
it has been reloaded for the second or subsequent times?” This question gets
resolved through the use of a special flag called “Postback”. We want to know
this because we would not want to clear out information on the page if it is
Still with me? Good. Let’s move on to something better.
There is an architecture called the .NET Page Life Cycle. This
is a framework that identifies the different stages that every .NET page goes
through, from birth to death. At a high level these stages are:
||The page request occurs
before the page life cycle begins. When the page is requested by a user,
ASP.NET determines whether the page needs to be parsed and compiled
(therefore beginning the life of a page), or whether a cached version of the
page can be sent in response without running the page.
||In the start stage, page
properties such as Request and Response are set. At this stage, the page also determines whether the
request is a postback or a new request and sets the IsPostBack property. The page also sets the UICulture property.
||During page initialization,
controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to
the page if applicable. If the current request is a postback, the postback
data has not yet been loaded and control property values have not been restored
to the values from view state.
||During load, if the current
request is a postback, control properties are loaded with information
recovered from view state and control state.
|Postback event handling
||If the request is a
postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
||Before rendering, view state
is saved for the page and all controls. During the rendering stage, the page
calls the Render method for each control, providing a text writer that writes
its output to the OutputStream object of the page's Response property.
||The Unload event is raised after the page has been fully rendered, sent to
the client, and is ready to be discarded. At this point, page properties such
as Response and Request are unloaded and cleanup is performed.
Important Events to know about include:
Each of these events are important! For example, during the
Init event, I might want to initialize some variables or controls on the
LoadData event would be a good place to load data into your
page controls. And of course the PreRender event is a good place to
hide or display controls on the page, based upon conditional logic.
So now we know a little more about the underpinnings of the
.NET code model. There is much more to know and learn, but now you have the
basics. Modern code generation tools like Iron Speed Designer can help take
the pain away. Iron Speed for example will generate all of the code for us,
leaving us with just the work to implement custom business logic, if required.
I often use Iron Speed Designer to generate the application
for me (from my pre-designer database), and then use Visual Studio 2010 if I
need to code something special.
Gaining a solid grasp of the .Net Page Life Cycle will go a
long ways towards improving your understanding (and your patience) when working
The best way to learn more about the .NET Page Life Cycle,
and of course by extension the Code Model is to use free tools. You can
download both Iron Speed Designer and Visual Studio 2010 Express Edition for
free. Generate sample ASP.NET applications in Iron Speed, and then look at the
generated pages in Visual Studio. You will be pleased with the results.