Search code examples
c#asp.netviewstate

How can I read the value of a ViewState into a variable?


I have a dictionary declared in the field such as:

Dictionary<String, string[]> operationDetails = new Dictionary<string, string[]> { };

Some operations are carried out and the dictionary is stored back into a ViewState like so:

ViewState.Add("operationDetails", operationDetails);

I now want to read this variable and store it back into operationDetails:

operationDetails = ViewState["operationDetails"];

However, this returns me with the error of unable to convert object to dictionary. I have tried Convert.ToDictionary but such a statement does not exist.

How can I read this view state back into a variable?


Solution

  • You have to cast the ViewState back to it's original DataType

    operationDetails = ViewState["operationDetails"] as Dictionary<String, string[]>;