Thoughts on the ASP.NET ViewState
Last week I've attended the DevReach conference in Sofia, Bulgaria. It is one of the few international events presented in South Eastern Europe, but a lot of famous international speakers took part in it and I would say it was a good conference. I met a number of well-known faces from the .NET community worldwide and I also had the chance to interview Lino Tadros, Chad Hower a.k.a Kudzu and Todd Anglin (big thanks guys!) for SilverlightShow.net.
One of the sessions I attended was about Tips & Tricks for Managing ViewState (by Todd Anglin). It made me acquire a deeper knowledge on this 'magic' StateBag. A lot of developers, not only junior, are confused about how the ViewState actually works. If you google about ViewState, you will find a couple of articles that explain the 'magic' – some of them good, some of them not so. I strongly recommend reading the blog of Dave Reed (Infinities Loop) TRULY Understanding ViewState if you want to truly understand it ;)
The thing in Todd Anglin's session which provoked me to blog about is the way he programmatically set control properties – in the PreInit stage. Just to give you an overview of the ViewState lifecycle, I'll say that it is loaded in the Init stage and this is exactly where TrackViewState() is called. After that, if a ViewState value is changed, it is considered dirty and StateBag.SaveViewState() will save and return the new value when its state is requested. Having this in mind you can think of changing control properties at the PreInit event. Yes, that is great place. They won't be marked as dirty because TrackViewState() is not still called.
So if you have a code like the following your ViewState won't suffer.
protected override void OnPreInit( EventArgs e )
litMessage.Text = "Hey, my ViewState is less than before! Ha!";
base.OnPreInit( e );
Just as simple as that. Then, why do we continue to see lot of ASP.NET tutorials, articles, blogs, etc. that initialize control properties at the Load event? Frequently I see code like this:
protected void Page_Load( object sender, EventArgs e )
if ( !IsPostBack )
gridData.DataSource = myDataCollection;
What is the size of myDataCollection? 10kb? 20kb? 120kb? Depends on the data. But why, even if it is 10bytes don't you put it in the PreInit event? Hey, when we are talking about public web applications, that matters! Notice that the next time you set default values, bind data, etc.
In case you use a MasterPage you can't use this tip. MasterPage is treated like a control in the ContentPage and all the controls on the page aren't still initialized at this stage. The simplest thing you can do is to wire up the Init event of the control and there to set the properties you want. Of course, you can use this trick even you have no MasterPage but you don't want to wire up the Init event of all 100 controls you have on the page, do you? ;)
As this is my first contribution to the ASP.NET community, I hope I've helped you guys and I'll be glad to have your feedback.