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.
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.
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.
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.
In C#, Dictionary is a generic collection which is generally used to store key/value pairs.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With