3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web.UI
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
29 * Contact: <gvaish@iitk.ac.in>
32 * (C) Gaurav Vaish (2001)
37 using System
.Collections
;
38 using System
.Collections
.Specialized
;
40 namespace System
.Web
.UI
42 public sealed class StateBag
: IStateManager
, IDictionary
, ICollection
, IEnumerable
44 private bool ignoreCase
;
46 private HybridDictionary bag
;
48 public StateBag (bool ignoreCase
)
50 Initialize (ignoreCase
);
58 private void Initialize (bool ignoreCase
)
60 this.ignoreCase
= ignoreCase
;
62 bag
= new HybridDictionary (ignoreCase
);
66 get { return bag.Count; }
70 public object this [string key
] {
72 if (key
== null || key
.Length
== 0)
73 throw new ArgumentException (HttpRuntime
.FormatResourceString ("Key_Cannot_Be_Null"));
75 object val
= bag
[key
];
78 return ((StateItem
) val
).Value
;
83 set { Add (key, value); }
86 object IDictionary
.this [object key
] {
87 get { return this [(string) key] as object; }
89 set { Add ((string) key, value); }
92 public ICollection Keys
{
93 get { return bag.Keys; }
96 public ICollection Values
{
97 get { return bag.Values; }
100 public StateItem
Add (string key
, object value)
102 if (key
== null || key
.Length
== 0)
103 throw new ArgumentException (HttpRuntime
.FormatResourceString ("Key_Cannot_Be_Null"));
105 StateItem val
= bag
[key
] as StateItem
; //don't throw exception when null
107 if(value != null || marked
) {
108 val
= new StateItem (value);
112 else if (value == null && !marked
)
117 if (val
!= null && marked
) {
129 public IDictionaryEnumerator
GetEnumerator ()
131 return bag
.GetEnumerator ();
134 public bool IsItemDirty (string key
)
136 object o
= bag
[key
];
139 return ((StateItem
) o
).IsDirty
;
144 public void Remove (string key
)
152 public void SetItemDirty (string key
, bool dirty
)
154 if (bag
[key
] is StateItem
)
155 ((StateItem
) bag
[key
]).IsDirty
= dirty
;
158 internal bool IsTrackingViewState
{
159 get { return marked; }
162 internal void LoadViewState (object state
)
165 Pair pair
= (Pair
) state
;
166 ArrayList keyList
= (ArrayList
) (pair
.First
);
167 ArrayList valList
= (ArrayList
) (pair
.Second
);
169 int valCount
= valList
.Count
;
170 for(int i
= 0; i
< keyList
.Count
; i
++) {
172 Add ((string) keyList
[i
], valList
[i
]);
174 Add ((string) keyList
[i
], null);
179 internal object SaveViewState ()
182 ArrayList keyList
= null, valList
= null;
184 foreach (string key
in bag
.Keys
) {
185 StateItem item
= (StateItem
) bag
[key
];
188 if (keyList
== null) {
189 keyList
= new ArrayList ();
190 valList
= new ArrayList ();
194 valList
.Add (item
.Value
);
199 return new Pair (keyList
, valList
);
204 internal void TrackViewState()
209 IEnumerator IEnumerable
.GetEnumerator ()
211 return GetEnumerator ();
214 void IStateManager
.LoadViewState (object savedState
)
216 LoadViewState (savedState
);
219 object IStateManager
.SaveViewState ()
221 return SaveViewState ();
224 void IStateManager
.TrackViewState ()
229 bool IStateManager
.IsTrackingViewState
{
230 get { return IsTrackingViewState; }
233 void ICollection
.CopyTo (Array array
, int index
)
235 Values
.CopyTo (array
, index
);
238 bool ICollection
.IsSynchronized
{
239 get { return false; }
242 object ICollection
.SyncRoot
247 void IDictionary
.Add (object key
, object value)
249 Add ((string) key
, value);
252 void IDictionary
.Remove (object key
)
254 Remove ((string) key
);
257 bool IDictionary
.Contains (object key
)
259 return bag
.Contains ((string) key
);
262 bool IDictionary
.IsFixedSize
{
263 get { return false; }
266 bool IDictionary
.IsReadOnly
{
267 get { return false; }