Ralph Varjabedian
Coding is a systematic art

To ViewState or not to ViewState

Wednesday, April 16, 2008 2:16 PM

ViewState is used inside your controls mostly for keeping track of variables on PostBacks. So basically if you have a variable or a property inside your WebUserControl and you want it to persist values, this is when you use the ViewState. However it is important to understand that the ViewState data is put in the page response itself as a Hidden field, what does this mean?

This means that first, you can not trust the input from your ViewState, as it comes back with the page post so it can be changed. By default the ViewState is not encrypted, however you can instruct IIS to encrypt it but it will affect performance. For example if you put in your ViewState an ID of a record to update, do not update it without asserting that it actually belongs to the logged in user, a full security discussion is beyond this post.

Second this means that your page's response size will grow as the data you save in your ViewState grows; this affects performance as it will take more time for your end user's browser to load the page and it will take more time when he wants to post back to your server. If you want to check this, when you get your page from your server, save it as an html file from your browser and check its size. There is also some extra processing in Serializing/Deserializing the ViewState. So in short, the more you save data in your ViewState, the more your suffer from performance.

So what happens when you have some large data that you have to save in your ViewState and there isn't a better solution for it. You can use a combination of your ViewState and Session to give you the same results, but with much lesser performance degradation. The trick is to save your large data inside your session with a certain generated key and just save that key inside your ViewState. For an example, suppose you have a DataSet that you want to keep track on post backs, you write some code like this.

public DataSet SavedDataSource
{
    get
    {
        if (ViewState["SavedDataSourceKey"] == null)
            return null;

        return (DataSet)Session[(string)ViewState["SavedDataSourceKey"]];
    }

    set
    {
        if (ViewState["SavedDataSourceKey"] == null && value == null)
            return;

        if (ViewState["SavedDataSourceKey"] == null)
            ViewState["SavedDataSourceKey"] = System.Guid.NewGuid().ToString();

        if (value == null)
        {
            Session.Remove((string)ViewState["SavedDataSourceKey"]);
            ViewState.Remove("SavedDataSourceKey");
        }
        else
            Session[(string)ViewState["SavedDataSourceKey"]] = value;
    }
}

By using your Session to save your large data, you are saving on the response size and also on the PostBack size, and your data is kept on your server.

A word of caution:

Please note that you are now saving your data in your Session, this means you have to be careful not to overuse this method excessively as your Session will grow on the server per user, so do not use this method frequently unless you need to use this trick when you have large data. Moreover if you know when you can dispose of your variable, do it as soon as you can by setting your property to null, this will release the session data. For example if you know you are navigating to another page and you will no longer get PostBacks on your page, then set the SavedDataSource to null to release the Session data.


Feedback

# re: To ViewState or not to ViewState

Hi, Ralph!

This is excellent article of the topic I am always interested in.
I just thought maybe it's not such a good idea to preserve large data in the session; Sessions are fragile, aren't they?
Other than that, I like your article very much!

Best regards
Imbrod

P.S. I know there is lot of s**t happening in your country again, so hang in there! You are not alone! (I'm from Croatia so I remember what's it like...)
Take care and best wishes! 5/10/2008 11:55 PM | Imbrod

# re: To ViewState or not to ViewState

Hello Imbrod,

Thank you for your feedback. Yes it is true that it is not a good idea to put large amount of data in the session as the resources are limited. However the post was about Viewstate vs. Session and the amount of data is relative, in this context, the Session can accept larger amounts of data than the ViewState since the ViewState has it's major penalties.

And also thank you a lot for the encouragement. We kind of got used to this in Lebanon and we manage well :) In the area that I am living there is nothing going on, but we can not go to work easily but we connect from Home and work from there. How did you know about our situation?

Let me know if there are any specific .NET Topics you are interested in.
Best Regards. 5/11/2008 8:10 PM | Ralph Varjabedian

# re: To ViewState or not to ViewState

I now feel like I'm being "upsold". I found this page whilst googling for a solution to a recently surfacing viewstate error - and am even considering turning it off to "see what happens" (I'm racking my brains to think, did I actually use viewstate anywhere??). 12/14/2009 1:06 PM | http://www.casino4china.com

# re: To ViewState or not to ViewState

Good work 12/22/2009 9:00 AM | Shared Web Hosting

# re: To ViewState or not to ViewState

Well, if you are going to develop your own server side controls or even composite user controls, most probably you will have to use ViewState sooner or later. Turning it off is not a good idea as most ASP.NET Controls will use it, unless you have a simple page. The whole point to remember is not to fill lots of info there because it increases the page's size, hence increases network latency and your pages will be slower and your server will suffer more. 12/22/2009 1:10 PM | Ralph Varjabedian

Post a comment





 

Please add 8 and 8 and type the answer here: