Skip to main content
.NET Framework Class Library
StateBag Class

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

Inheritance Hierarchy
SystemObject
  System.Web.UIStateBag

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax
Public NotInheritable Class StateBag _
	Implements [%$TOPIC/x317kh3a_en-us_VS_110_2_0_0_0_0%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_0_0_1%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_0_0_2%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_0_0_3%]
public sealed class StateBag : [%$TOPIC/x317kh3a_en-us_VS_110_2_0_1_0_0%], 
	[%$TOPIC/x317kh3a_en-us_VS_110_2_0_1_0_1%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_1_0_2%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_1_0_3%]
public ref class StateBag sealed : [%$TOPIC/x317kh3a_en-us_VS_110_2_0_2_0_0%], 
	[%$TOPIC/x317kh3a_en-us_VS_110_2_0_2_0_1%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_2_0_2%], [%$TOPIC/x317kh3a_en-us_VS_110_2_0_2_0_3%]
[<[%$TOPIC/x317kh3a_en-us_VS_110_2_0_3_0_0%]>]
type StateBag =  
    class 
        interface [%$TOPIC/x317kh3a_en-us_VS_110_2_0_3_0_1%] 
        interface [%$TOPIC/x317kh3a_en-us_VS_110_2_0_3_0_2%] 
        interface [%$TOPIC/x317kh3a_en-us_VS_110_2_0_3_0_3%] 
        interface [%$TOPIC/x317kh3a_en-us_VS_110_2_0_3_0_4%] 
    end

The StateBag type exposes the following members.

Constructors
  NameDescription
Public method StateBagInitializes a new instance of the StateBag class. This is the default constructor for this class.
Public method StateBag(Boolean)Initializes a new instance of the StateBag class that allows stored state values to be case-insensitive.
Top
Properties
  NameDescription
Public property CountGets the number of StateItem objects in the StateBag object.
Public property ItemGets or sets the value of an item stored in the StateBag object.
Public property KeysGets a collection of keys representing the items in the StateBag object.
Public property ValuesGets a collection of the view-state values stored in the StateBag object.
Top
Methods
  NameDescription
Public method AddAdds 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 method ClearRemoves all items from the current StateBag object.
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public method GetEnumeratorReturns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.
Public method GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsItemDirtyChecks a StateItem object stored in the StateBag object to evaluate whether it has been modified since the call to ControlTrackViewState.
Public method RemoveRemoves the specified key/value pair from the StateBag object.
Public method SetDirtySets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.
Public method SetItemDirtySets the Dirty property for the specified StateItem object in the StateBag object.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension Method AsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method CastTResultCasts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfTypeTResultFilters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method ICollectionCopyToFor a description of this member, see ICollectionCopyTo.
Explicit interface implemetation Private property ICollectionIsSynchronizedFor a description of this member, see ICollectionIsSynchronized.
Explicit interface implemetation Private property ICollectionSyncRootFor a description of this member, see ICollectionSyncRoot.
Explicit interface implemetation Private method IDictionaryAddFor a description of this member, see IDictionaryAdd.
Explicit interface implemetation Private method IDictionaryContainsFor a description of this member, see IDictionaryContains.
Explicit interface implemetation Private property IDictionaryIsFixedSizeFor a description of this member, see IDictionaryIsFixedSize.
Explicit interface implemetation Private property IDictionaryIsReadOnlyFor a description of this member, see IDictionaryIsReadOnly.
Explicit interface implemetation Private property IDictionaryItemFor a description of this member, see IDictionaryItem.
Explicit interface implemetation Private method IDictionaryRemoveFor a description of this member, see Remove.
Explicit interface implemetation Private method IEnumerableGetEnumeratorFor a description of this member, see IEnumerableGetEnumerator.
Explicit interface implemetation Private property IStateManagerIsTrackingViewStateGets a value indicating whether state changes are being tracked.
Explicit interface implemetation Private method IStateManagerLoadViewStateRestores the previously saved view state of the StateBag object.
Explicit interface implemetation Private method IStateManagerSaveViewStateSaves the changes to the StateBag object since the time the page was posted back to the server.
Explicit interface implemetation Private method IStateManagerTrackViewStateCauses the StateBag object to track changes to its state so that it can be persisted across requests.
Top
Remarks

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

Examples

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 ControlRender method is called on the control.

' This control renders values stored in view state for Text and FontSize properties. 

Imports System
Imports System.Web
Imports System.Web.UI

Namespace ViewStateControlSamples

    Public Class CustomLabel : Inherits Control
        Private Const defaultFontSize As Integer = 3

        ' Add property values to view state with set;  
        ' retrieve them from view state with get. 
        Public Property [Text]() As String 
            Get 
                Dim o As Object = ViewState("Text")
                If (IsNothing(o)) Then 
                    Return String.Empty
                Else 
                    Return CStr(o)
                End If 
            End Get 
            Set(ByVal value As String)
                ViewState("Text") = value
            End Set 
        End Property 


        Public Property FontSize() As Integer 
            Get 
                Dim o As Object = ViewState("FontSize")
                If (IsNothing(o)) Then 
                    Return defaultFontSize
                Else 
                    Return CInt(ViewState("FontSize"))
                End If 

            End Get 
            Set(ByVal value As Integer)
                ViewState("FontSize") = value
            End Set 
        End Property
        <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub Render(ByVal Output As HtmlTextWriter)
            Output.Write("<font size=" & Me.FontSize & ">" & Me.Text & "</font>")
        End Sub 

    End Class 

End Namespace
// 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>");
        }
    }
}
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

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.

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