Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing Dictionary<string, string> in ASP.NET view state?

I'm trying to store a Dictionary<string, string> in the ViewState of a custom control I'm developing for ASP.NET 2.0:

private Dictionary<String, String> Items
    {
        get
        {
            object d = ViewState["Items"];
            return (d == null ? null : (Dictionary<String, String>)d);
        }
        set
        {
            ViewState["Items"] = value;
        }
    }

Accessing it looks like this:

public void UpdateData
{
    if (this.Items == null)
        this.Items = new Dictionary<string, string>();
    else
        this.Items.Clear();
    //Fill the collection
}

When it gets set the first time the page loads, it appears to work fine. But on subsequent postbacks, the value returned is always null (the first condition always happens). Debugging shows that it's getting null out of the ViewState in the property get.

I've done some research and have found that classes must implement IStateManager to be saveable in ViewState, and the Dictionary MSDN page appears to indicate that Dictionary<TKey, TValue> does not. But I've stored dictionaries before in ViewState without a problem. What's going on here? Was my previous experience a fluke?

UPDATE: I tried adding some test code to the property: ViewState["ItemTest"] = "foo"; in the set and string test = (string)ViewState["ItemTest"]; in the get. Like the Dictionary, it comes out null. So it doesn't appear to be a problem with the Dictionary being serializable. Also, to clarify, UpdateData is called from my RenderControl override, which happens after Page_Load in the page that contains the control.

like image 552
Tom Hamming Avatar asked Jun 09 '11 17:06

Tom Hamming


People also ask

What kind of data we can store in ViewState?

Data objects such as hash tables, strings, array objects, array list objects, Boolean values and custom-type converters can be stored in view state. View state is ideally used when the data to be preserved is relatively small and the data need not be secured.

Can we store object in ViewState?

Yes. We can store object in ViewState as it stores the data in the string form although it works like dictionary. Just serialize the object and store the string in ViewState.

How does the ViewState stores the values?

View State is one of the methods of the ASP.NET page framework used to preserve and store the page and control values between round trips. It is maintained internally as a hidden field in the form of an encrypted value and a key. Default enables the View State for a page.

What is dictionary string string in C#?

In C#, Dictionary is a generic collection which is generally used to store key/value pairs.


2 Answers

You can store the dictionary in ViewState, but you are attempting to do this too late in the page life cycle. Just as ViewState is loaded after Init, ViewState is saved before controls are rendered. Move your logic out of RenderControl and into another method or event handler earlier in the life cycle, such as PreRender.

protected override void OnPreRender(EventArgs e)
{
    if (this.Items == null)
    {
        this.Items = new Dictionary<string, string>();
    }

    base.OnPreRender(e);
}

You will notice the that object is no longer null on subsequent postbacks as long as ViewState is not being disabled on either the control or its parent.

like image 161
Anthony Pegram Avatar answered Oct 23 '22 14:10

Anthony Pegram


Make sure you're not accessing the property too early in the page lifecycle as the viewstate is not loaded directly.

From what method (at which point in the page lifecycle) are you requesting this property for the first time?

like image 43
thekip Avatar answered Oct 23 '22 12:10

thekip