Saving the <string, string> dictionary in ASP.NET view state?

I am trying to save Dictionary<string, string>in a ViewState a custom control that I am 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;
        }
    }

Access to it is as follows:

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

When it is installed the first time the page is launched, it works fine. But with subsequent postbacks, the return value is always null (the first condition is always true). Debugging shows what it gets nullfrom the ViewState in the property get.

, IStateManager ViewState, MSDN " , Dictionary<TKey, TValue> . ViewState . ? ?

UPDATE: : ViewState["ItemTest"] = "foo"; set string test = (string)ViewState["ItemTest"]; get. Dictionary, null. , Dictionary. , , UpdateData RenderControl, Page_Load , .

+5
2

ViewState, . , ViewState Init, ViewState . RenderControl , PreRender.

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

    base.OnPreRender(e);
}

, null postbacks, ViewState , .

+8

, , .

( ) ?

+1

All Articles