1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
|
//------------------------------------------------------------------------------
// <copyright file="StateBag.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI {
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Web.Util;
/*
* The StateBag class is a helper class used to manage state of properties.
* The class stores name/value pairs as string/object and tracks modifications of
* properties after being 'marked'. This class is used as the primary storage
* mechanism for all HtmlControls and WebControls.
*/
/// <devdoc>
/// <para>Manages the state of Web Forms control properties. This
/// class stores attribute/value pairs as string/object and tracks changes to these
/// attributes, which are treated as properties, after the Page.Init
/// method is executed for a
/// page request. </para>
/// <note type="note">
/// Only values changed after Page.Init
/// has executed are persisted to the page's view state.
/// </note>
/// </devdoc>
public sealed class StateBag : IStateManager, IDictionary {
private IDictionary bag;
private bool marked;
private bool ignoreCase;
/*
* Constructs an StateBag
*/
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.StateBag'/> class.</para>
/// </devdoc>
public StateBag() : this(false) {
}
/*
* Constructs an StateBag
*/
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.StateBag'/> class that allows stored state
/// values to be case-insensitive.</para>
/// </devdoc>
public StateBag(bool ignoreCase) {
marked = false;
this.ignoreCase = ignoreCase;
bag = CreateBag();
}
/*
* Return count of number of StateItems in the bag.
*/
/// <devdoc>
/// <para>Indicates the number of items in the <see cref='System.Web.UI.StateBag'/> object. This property is
/// read-only.</para>
/// </devdoc>
public int Count {
get {
return bag.Count;
}
}
/*
* Returns a collection of keys.
*/
/// <devdoc>
/// <para>Indicates a collection of keys representing the items in the <see cref='System.Web.UI.StateBag'/> object.
/// This property is read-only.</para>
/// </devdoc>
public ICollection Keys {
get {
return bag.Keys;
}
}
/*
* Returns a collection of values.
*/
/// <devdoc>
/// <para>Indicates a collection of view state values in the <see cref='System.Web.UI.StateBag'/> object.
/// This property is read-only.</para>
/// </devdoc>
public ICollection Values {
get {
return bag.Values;
}
}
/*
* Get or set value of a StateItem.
* A set will automatically add a new StateItem for a
* key which is not already in the bag. A set to null
* will remove the item if set before mark, otherwise
* a null set will be saved to allow tracking of state
* removed after mark.
*/
/// <devdoc>
/// <para> Indicates the value of an item stored in the
/// <see langword='StateBag'/>
/// object. Setting this property with a key not already stored in the StateBag will
/// add an item to the bag. If you set this property to <see langword='null'/> before
/// the TrackState method is called on an item will remove it from the bag. Otherwise,
/// when you set this property to <see langword='null'/>
/// the key will be saved to allow tracking of the item's state.</para>
/// </devdoc>
public object this[string key] {
get {
if (String.IsNullOrEmpty(key))
throw ExceptionUtil.ParameterNullOrEmpty("key");
StateItem item = bag[key] as StateItem;
if (item != null)
return item.Value;
return null;
}
set {
Add(key,value);
}
}
/*
* Private implementation of IDictionary item accessor
*/
/// <internalonly/>
object IDictionary.this[object key] {
get { return this[(string) key]; }
set { this[(string) key] = value; }
}
private IDictionary CreateBag() {
return new HybridDictionary(ignoreCase);
}
/*
* Add a new StateItem or update an existing StateItem in the bag.
*/
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public StateItem Add(string key,object value) {
if (String.IsNullOrEmpty(key))
throw ExceptionUtil.ParameterNullOrEmpty("key");
StateItem item = bag[key] as StateItem;
if (item == null) {
if (value != null || marked) {
item = new StateItem(value);
bag.Add(key,item);
}
}
else {
if (value == null && !marked) {
bag.Remove(key);
}
else {
item.Value = value;
}
}
if (item != null && marked) {
item.IsDirty = true;
}
return item;
}
/*
* Private implementation of IDictionary Add
*/
/// <internalonly/>
void IDictionary.Add(object key,object value) {
Add((string) key, value);
}
/*
* Clear all StateItems from the bag.
*/
/// <devdoc>
/// <para>Removes all controls from the <see cref='System.Web.UI.StateBag'/> object.</para>
/// </devdoc>
public void Clear() {
bag.Clear();
}
/*
* Get an enumerator for the StateItems.
*/
/// <devdoc>
/// <para>Returns an enumerator that iterates over the key/value pairs stored in
/// the <see langword='StateBag'/>.</para>
/// </devdoc>
public IDictionaryEnumerator GetEnumerator() {
return bag.GetEnumerator();
}
/*
* Return the dirty flag of the state item.
* Returns false if there is not an item for given key.
*/
/// <devdoc>
/// <para>Checks an item stored in the <see langword='StateBag'/> to see if it has been
/// modified.</para>
/// </devdoc>
public bool IsItemDirty(string key) {
StateItem item = bag[key] as StateItem;
if (item != null)
return item.IsDirty;
return false;
}
/*
* Return true if 'marked' and state changes are being tracked.
*/
/// <devdoc>
/// <para>Determines if state changes in the StateBag object's store are being tracked.</para>
/// </devdoc>
internal bool IsTrackingViewState {
get {
return marked;
}
}
/*
* Restore state that was previously saved via SaveViewState.
*/
/// <devdoc>
/// <para>Loads the specified previously saved state information</para>
/// </devdoc>
internal void LoadViewState(object state) {
if (state != null) {
ArrayList data = (ArrayList)state;
for (int i = 0; i < data.Count; i += 2) {
#if OBJECTSTATEFORMATTER
string key = ((IndexedString)data[i]).Value;
#else
string key = (string)data[i];
#endif
object value = data[i + 1];
Add(key, value);
}
}
}
/*
* Start tracking state changes after "mark".
*/
/// <devdoc>
/// <para>Initiates the tracking of state changes for items stored in the
/// <see langword='StateBag'/> object.</para>
/// </devdoc>
internal void TrackViewState() {
marked = true;
}
/*
* Remove a StateItem from the bag altogether regardless of marked.
* Used internally by controls.
*/
/// <devdoc>
/// <para>Removes the specified item from the <see cref='System.Web.UI.StateBag'/> object.</para>
/// </devdoc>
public void Remove(string key) {
bag.Remove(key);
}
/*
* Private implementation of IDictionary Remove
*/
/// <internalonly/>
void IDictionary.Remove(object key) {
Remove((string) key);
}
/*
* Return object containing state that has been modified since "mark".
* Returns null if there is no modified state.
*/
/// <devdoc>
/// <para>Returns an object that contains all state changes for items stored in the
/// <see langword='StateBag'/> object.</para>
/// </devdoc>
internal object SaveViewState() {
ArrayList data = null;
//
if (bag.Count != 0) {
IDictionaryEnumerator e = bag.GetEnumerator();
while (e.MoveNext()) {
StateItem item = (StateItem)(e.Value);
if (item.IsDirty) {
if (data == null) {
data = new ArrayList();
}
#if OBJECTSTATEFORMATTER
data.Add(new IndexedString((string)e.Key));
#else
data.Add(e.Key);
#endif
data.Add(item.Value);
}
}
}
return data;
}
/// <devdoc>
/// Sets the dirty state of all item values currently in the StateBag.
/// </devdoc>
public void SetDirty(bool dirty) {
if (bag.Count != 0) {
foreach (StateItem item in bag.Values) {
item.IsDirty = dirty;
}
}
}
/*
* Internal method for setting dirty flag on a state item.
* Used internallly to prevent state management of certain properties.
*/
/// <internalonly/>
/// <devdoc>
/// </devdoc>
public void SetItemDirty(string key,bool dirty) {
StateItem item = bag[key] as StateItem;
if (item != null) {
item.IsDirty = dirty;
}
}
/// <internalonly/>
bool IDictionary.IsFixedSize {
get { return false; }
}
/// <internalonly/>
bool IDictionary.IsReadOnly {
get { return false; }
}
/// <internalonly/>
bool ICollection.IsSynchronized {
get { return false;}
}
/// <internalonly/>
object ICollection.SyncRoot {
get {return this; }
}
/// <internalonly/>
bool IDictionary.Contains(object key) {
return bag.Contains((string) key);
}
/// <internalonly/>
IEnumerator IEnumerable.GetEnumerator() {
return ((IDictionary)this).GetEnumerator();
}
/// <internalonly/>
void ICollection.CopyTo(Array array, int index) {
Values.CopyTo(array, index);
}
/*
* Return true if tracking state changes.
* Method of private interface, IStateManager.
*/
/// <internalonly/>
bool IStateManager.IsTrackingViewState {
get {
return IsTrackingViewState;
}
}
/*
* Load previously saved state.
* Method of private interface, IStateManager.
*/
/// <internalonly/>
void IStateManager.LoadViewState(object state) {
LoadViewState(state);
}
/*
* Start tracking state changes.
* Method of private interface, IStateManager.
*/
/// <internalonly/>
void IStateManager.TrackViewState() {
TrackViewState();
}
/*
* Return object containing state changes.
* Method of private interface, IStateManager.
*/
/// <internalonly/>
object IStateManager.SaveViewState() {
return SaveViewState();
}
}
}
|