blog.mha.dk
The on-line blog of Michael Holm Andersen

ViewState trimming with automatic state restore

Tuesday, 13 May 2008 07:28 by mha

A quick follow up on the last article about trimming ViewState. I was looking at the code I posted a couple of days ago and came to think about the life cycle of a ASP.NET page .. more precisly when in the page flow the ViewState data is actually restored. I checked MSDN and found out that this occurs right after the PreLoad event and just before the Load event.

With this information (and the knowledge that controls such as dropdownlist and textbox restore their posted state (the selected item of a dropdown is 'posted') even when ViewState is disabled), if I simply moved my databind of the control to an event BEFORE the .NET framework restores the view state - the control (in this case a DropDownList) would remain it selected value on Page_Load - in other words: Now the dropdown will behave just like it normally would, only my rather large list of data will NOT be persisted into the ViewState hidden field! Awesome!!

So .. in short - why does this works: I'm populating the dropdown list with data BEFORE it attempts to load it's posted value.

Pretty nice, ehh :)  .. and yes, of course - to sum it all up. The code: 

Comments are closed