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;

gridData.DataBind();

}

}

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.

8 Comments

  • "In case you use a MasterPage you can't use this tip." You answered your own question. Aside from the occasional one-offs like login pages and such, I can't remember the last time that I didn't use a Master Page in an ASP.NET 2.0 (or later) site.

  • Thanks, Bas that is correct. Initialization and data binding are two different operations.
    Consider you have a DropDownList and you bind it to a collection at Page_Load. This way the data is persisted into the __ViewState hidden field. Now every time you do a post back this data stays encoded in the __ViewState field and you download it again and again. I'll use the trite example where you bind to all the 50 US states stored in a SQL database. Your query probably looks like this:

    select Id, Name from USStates

    Believe it or not to query this data from the database is faster than persisting it into the ViewState. Modern databases have caching algorithms that do the job for you.

    If we are talking about time-consuming database (or IO for example) operations then you should choose the ViewState (or other state management mechanism) to store your data.

  • usarskyy,

    Yes, if you follow the comments thread you will see the trackback from ysolodkyy (It Could Be Done!) where he already points to its article. Thanks anyway.

  • you are a slow walker,but I never walk backwards.

    -----------------------------------

  • The wealth of the mind is the only wealth.

    -----------------------------------

  • -----------------------------------------------------------
    their is really a problem inside the very first spot.

  • -----------------------------------------------------------
    Hello webmaster I love your submit ….

  • I lately came across your web site and have been reading a great deal of posts of yours. I just thought I'd add a fast comment and let you know that you have got a truly nice weblog. I'll watch out for updates from you!

Comments have been disabled for this content.