Export (0) Print
Expand All

StateBag Class

Manages the view state of ASP.NET server controls, including pages. This class cannot be inherited.

System.Object
  System.Web.UI.StateBag

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class StateBag : IStateManager, 
	IDictionary, ICollection, IEnumerable

The StateBag type exposes the following members.

  NameDescription
Public methodStateBag()Initializes a new instance of the StateBag class. This is the default constructor for this class.
Public methodStateBag(Boolean)Initializes a new instance of the StateBag class that allows stored state values to be case-insensitive.
Top

  NameDescription
Public propertyCountGets the number of StateItem objects in the StateBag object.
Public propertyItemGets or sets the value of an item stored in the StateBag object.
Public propertyKeysGets a collection of keys representing the items in the StateBag object.
Public propertyValuesGets a collection of the view-state values stored in the StateBag object.
Top

  NameDescription
Public methodAddAdds a new StateItem object to the StateBag object. If the item already exists in the StateBag object, this method updates the value of the item.
Public methodClearRemoves all items from the current StateBag object.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsItemDirtyChecks a StateItem object stored in the StateBag object to evaluate whether it has been modified since the call to Control.TrackViewState.
Public methodRemoveRemoves the specified key/value pair from the StateBag object.
Public methodSetDirtySets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.
Public methodSetItemDirtySets the Dirty property for the specified StateItem object in the StateBag object.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodAsParallel()Overloaded. Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsParallel()Overloaded. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryable()Overloaded. Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodAsQueryable()Overloaded. (Defined by Queryable.)
Public Extension MethodCast<TResult>()Overloaded. Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodCast<TResult>()Overloaded. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>()Overloaded. Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>()Overloaded. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICollection.CopyToFor a description of this member, see ICollection.CopyTo.
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedFor a description of this member, see ICollection.IsSynchronized.
Explicit interface implemetationPrivate propertyICollection.SyncRootFor a description of this member, see ICollection.SyncRoot.
Explicit interface implemetationPrivate methodIDictionary.AddFor a description of this member, see IDictionary.Add.
Explicit interface implemetationPrivate methodIDictionary.ContainsFor a description of this member, see IDictionary.Contains.
Explicit interface implemetationPrivate propertyIDictionary.IsFixedSizeFor a description of this member, see IDictionary.IsFixedSize.
Explicit interface implemetationPrivate propertyIDictionary.IsReadOnlyFor a description of this member, see IDictionary.IsReadOnly.
Explicit interface implemetationPrivate propertyIDictionary.ItemFor a description of this member, see IDictionary.Item.
Explicit interface implemetationPrivate methodIDictionary.RemoveFor a description of this member, see Remove.
Explicit interface implemetationPrivate methodIEnumerable.GetEnumeratorFor a description of this member, see IEnumerable.GetEnumerator.
Explicit interface implemetationPrivate propertyIStateManager.IsTrackingViewStateGets a value indicating whether state changes are being tracked.
Explicit interface implemetationPrivate methodIStateManager.LoadViewStateRestores the previously saved view state of the StateBag object.
Explicit interface implemetationPrivate methodIStateManager.SaveViewStateSaves the changes to the StateBag object since the time the page was posted back to the server.
Explicit interface implemetationPrivate methodIStateManager.TrackViewStateCauses the StateBag object to track changes to its state so that it can be persisted across requests.
Top

View state for a page or control is the cumulative property values, or view, of that page or control. You can access this class through the Control.ViewState property. Controls can also store essential state information in control state, but that information is not stored as a StateBag object.

This class is the primary storage mechanism for all HTML and Web server controls. It stores attribute/value pairs as strings associated with the control. It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page's or control's view state.

This class implements a dictionary, and you can add items to it or remove items from it as you would any dictionary object. For more information about data collections, such as dictionaries, see Collections and Data Structures.

The following code example demonstrates a composite Label control that has Text and FontSize properties. These properties are saved to and retrieved from view state when the Control.Render method is called on the control.

// This control renders values stored in view state for Text and FontSize properties. 
using System;
using System.Web;
using System.Web.UI;

namespace ViewStateControlSamples
{

    public class CustomLabel : Control
    {
        private const int defaultFontSize = 3;

        // Add property values to view state with set; 
        // retrieve them from view state with get. 
        public String Text
        {
            get 
            { 
                object o = ViewState["Text"]; 
                return (o == null)? String.Empty : (string)o;
            }

            set
            {
                ViewState["Text"] = value;
            }
        }


        public int FontSize
        {
            get
            {
                object o = ViewState["FontSize"];
                return (o == null) ? defaultFontSize : (int)o;
            }
            set
            {
                ViewState["FontSize"] = value;
            }
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void Render(HtmlTextWriter output)
        {
            output.Write("<font size=" + this.FontSize.ToString() + ">" + this.Text + "</font>");
        }
    }
}

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft