Click here to Skip to main content
15,886,788 members
Articles / Web Development / ASP.NET
Article

Master the .NET Code Model

10 May 2011CPOL5 min read 17K   10   4
A brief introduction for the uninitiated about the importance of understanding the .NET Code model. This is meant to be an objective look at learning the .NET code model, with references to Iron Speed Designer and Visual Studio as part of this objective viewpoint.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

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.

image001.png

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 the use of Javascript (and by extension AJAX).  Confused? I know I was initially. 

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 spreadsheet program.

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 way.

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 hard disk.

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 not necessary!

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:

StageDescription
Page requestThe 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.
StartIn 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.
InitializationDuring 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.
LoadDuring load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handlingIf 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.
RenderingBefore 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.
UnloadThe 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.

Source: http://msdn.microsoft.com/en-us/library/ms178472.aspx#general_page_lifecycle_stages

Important Events to know about include:

Table 1

image002.png

Source: http://msdn.microsoft.com/en-us/library/ms178472.aspx#general_page_lifecycle_stages

Each of these events are important!  For example, during the Init event, I might want to initialize some variables or controls on the page.  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.

Conclusion

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 with .NET. 

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Milestone Software Inc.
Canada Canada
Miles is a senior consultant and principal of Milestone Software and has been providing his clients with the right solutions for over twenty five years. He was the first to offer formal Iron Speed Designer training, and has been coaching and mentoring Iron Speed users since 2005. Miles is an Iron Speed MVP who loves to help clients finish their projects. He is an experienced data modeler who believes in strong database architecture and a best practices approach to software development.

For more information, have a look here:

http://www.ironspeedmvp.com
http://www.ironspeed.com/mvp/MVPMilesGibson.aspx

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jay R. Wren17-May-11 5:55
Jay R. Wren17-May-11 5:55 
GeneralThe title is misleading... Pin
Xetrill10-May-11 10:24
Xetrill10-May-11 10:24 
GeneralRe: The title is misleading... Pin
Seishin#10-May-11 11:47
Seishin#10-May-11 11:47 
GeneralRe: The title is misleading... Pin
Huisheng Chen15-May-11 2:52
Huisheng Chen15-May-11 2:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.