I have a base class for some kind of user controls, and in that base class and in its inherited classes I want to store some properties. I use
protected override object SaveControlState() protected override void LoadControlState(object savedState)
methods to save or load my custom values. Since I can work only with 1 parameter of type object, I have to use some kind of list if I want to work with more than 1 variable.
I tried to do it with
[Serializable] public class ControlSate : Dictionary<string, object>, ISerializable { void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { } public ControlSate(SerializationInfo info, StreamingContext context) : base(info, context) { } public ControlSate() : base() { } }
but it didn’t work, and looking for a solution I have read that Dictionary is not serializable.
So, my question is what type should I use to work with a set of values in user control’s state?
While
LoadControlStatedoes pass you anObjectit is possible for thatObjectto be anObject[]. In other words you are more than welcome to store an array ofObjectsin ControlState. I also believe that ControlState is optimized to work with theSystem.Web.UI.Pairtype so you can create trees of objects in ControlState if you wish.Please read this excellent article for the best way of storing multiple values in ControlState.