StateBag Class
TOC
Collapse the table of content
Expand the table of content

StateBag Class

 

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

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

System.Object
  System.Web.UI.StateBag

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

NameDescription
System_CAPS_pubmethodStateBag()

Initializes a new instance of the StateBag class. This is the default constructor for this class.

System_CAPS_pubmethodStateBag(Boolean)

Initializes a new instance of the StateBag class that allows stored state values to be case-insensitive.

NameDescription
System_CAPS_pubpropertyCount

Gets the number of StateItem objects in the StateBag object.

System_CAPS_pubpropertyItem[String]

Gets or sets the value of an item stored in the StateBag object.

System_CAPS_pubpropertyKeys

Gets a collection of keys representing the items in the StateBag object.

System_CAPS_pubpropertyValues

Gets a collection of the view-state values stored in the StateBag object.

NameDescription
System_CAPS_pubmethodAdd(String, Object)

Adds 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.

System_CAPS_pubmethodClear()

Removes all items from the current StateBag object.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIsItemDirty(String)

Checks a StateItem object stored in the StateBag object to evaluate whether it has been modified since the call to Control.TrackViewState.

System_CAPS_pubmethodRemove(String)

Removes the specified key/value pair from the StateBag object.

System_CAPS_pubmethodSetDirty(Boolean)

Sets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.

System_CAPS_pubmethodSetItemDirty(String, Boolean)

Sets the Dirty property for the specified StateItem object in the StateBag object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

For a description of this member, see ICollection.CopyTo.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIDictionary.Add(Object, Object)

For a description of this member, see IDictionary.Add.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIDictionary.Contains(Object)

For a description of this member, see IDictionary.Contains.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIDictionary.Remove(Object)

For a description of this member, see Remove.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIEnumerable.GetEnumerator()

For a description of this member, see IEnumerable.GetEnumerator.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.LoadViewState(Object)

Restores the previously saved view state of the StateBag object.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.SaveViewState()

Saves the changes to the StateBag object since the time the page was posted back to the server.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIStateManager.TrackViewState()

Causes the StateBag object to track changes to its state so that it can be persisted across requests.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

For a description of this member, see ICollection.IsSynchronized.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

For a description of this member, see ICollection.SyncRoot.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIDictionary.IsFixedSize

For a description of this member, see IDictionary.IsFixedSize.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIDictionary.IsReadOnly

For a description of this member, see IDictionary.IsReadOnly.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIDictionary.Item[Object]

For a description of this member, see IDictionary.Item.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIStateManager.IsTrackingViewState

Gets a value indicating whether state changes are being tracked.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

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
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft