Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Page Lifecycle Study : Part One- The Project

This is one of those posts that pains me to write. I blame Monorail for the pain that I am now acutely aware of in working with the Web Forms model in ASP.NET. But that's a whole other post...

Dealing with the events that take place in the lifetime of a Page in WebForms is tricky once you get beyond the demo samples. Dynamic loading of controls based on state, collection control (ie, Repeater) value persistence between postbacks, and event timing of web controls immediately reveal to the excited "look I can drag a GridView and see my employees table" developer that he has just stepped in a big pile of over-architecture and got bizarre form behavior all over him. 

steppedonit

And that is with ViewState ON.

 

A wise blogger once wrote ViewState is the devil and shortly after that I was checking out Scott Watermasysk's ASP.NET tips and dealing with bloated pages in my own application...I was encouraged to turn ViewState OFF and deal with the consequences. I really HAD to since my ViewState had gotten out of control.

 

As if that didn't complicate matters enough, I was using the Model-View-Presenter pattern and wrestled with how to implement this with a stateless view (more or less) that has a complex strategy for restoring values from the Request.Form collection while making the view the entry point for requests, thus seemingly forcing my Presenter/Controller into submission to the famed ASP.NET Page Lifecycle...something I was hoping to avoid.

I decided it was time to just isolate all this and create a test bed where I could discover exactly what is happening when with actual controls and get feedback from this monster to tell me what it demands for me to simply pass data back and forth in a text stream. So I created this simple project that has quite a bit of sample data and different ways to see events firing during the postbacks. Controls like the Repeater and DropDownList are absolutely non-intuitive once ViewState is OFF and so are worth a close look on how to persist values. Also, the event handling of simple controls like the TextBox require some deeper understanding once you turn ViewState off.

I simply had quite a bit of difficulty finding actual code that showed things happening at different times when I was first swimming in the sea of events ASP.NET offers...all I could find were pretty diagrams and lots of code jammed in Page_Load with the occasional exotic mention of the 'catch-up' performed by controls upon Controls.Add to the control hierarchy.

 

So for this first post, here is the project .

DISCLAIMER: It's not pretty, but rather could maybe be useful for people who are confused about what is happening in their pages or to teach others.

I have since settled down quite a bit regarding how I prefer to implement MVP since I created this. I'll post more on what I do with MVP in the future.


Posted 10-04-2007 1:04 AM by Michael Nichols
Filed under: ,

[Advertisement]

Comments

Billy McCafferty wrote re: Page Lifecycle Study : Part One- The Project
on 10-04-2007 1:53 PM

ViewState really is the devil!  The only remedy I've found for keeping it on (in order to effectively use some third party controls such as telerik), is to enable HTTP compression from on the server.  But even then, ViewState is a ridiculously bloated way to simulate statefullness in a state-less environment.  You could alternatively store it on the server but it's klunky.

Dan wrote re: Page Lifecycle Study : Part One- The Project
on 10-04-2007 2:04 PM

I'm very impressed, excellent analysis of page lifecycle. Thanks for sharing

Brad Mead wrote re: Page Lifecycle Study : Part One- The Project
on 10-04-2007 3:21 PM

I love this!

I think it is so neccessary to hammer lifecylce home. I will be follwoing your posts with enthusiam to re/visit this.

PartialClass wrote re: Page Lifecycle Study : Part One- The Project
on 10-09-2007 2:14 AM

where are the details mike?

i didnt get a single LOC

Mike wrote re: Page Lifecycle Study : Part One- The Project
on 10-09-2007 11:42 AM

@Partial - I'm not sure I follow you. This is an introduction and you can download the project following the link above...there is at least one LOC in that :)

@Billy - I just turn ViewState off and deal with the consequences

@Dan,@Brad - Thanks...I'll do my best to explain what I have discovered and hope it helps someone else.

PartialClass wrote re: Page Lifecycle Study : Part One- The Project
on 10-10-2007 2:03 AM

i meant to say that  i didnt understand single LOC :)

Joe Chung wrote re: Page Lifecycle Study : Part One- The Project
on 10-18-2007 4:49 PM

I've seen a lot of people *** and moan about ViewState, and not a single one of them has ever developed an ASP.NET Web site worth a damn.

Robert F. wrote re: Page Lifecycle Study : Part One- The Project
on 10-18-2007 8:12 PM

When I try to build your solution, it says the ScriptManager namespace does not exist.  Am I missing something?

Michael Nichols wrote re: Page Lifecycle Study : Part One- The Project
on 10-19-2007 9:47 AM

@Robert - Sorry I had some references left in there that required the ASP.NET Web Extensions. I removed the extraneous references and re-uploaded. Plz try again.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)