This documentation is archived and is not being maintained.

StateItem Class

Represents an item that is saved in the StateBag class when view state information is persisted between Web requests. This class cannot be inherited.

For a list of all members of this type, see StateItem Members.


[Visual Basic]
NotInheritable Public Class StateItem
public sealed class StateItem
public __gc __sealed class StateItem
public class StateItem

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.


View state is the accumulation of a page's or an ASP.NET server control's property values and is sent to the requesting browser in a hidden field.

You can explicitly add StateItem objects to an ASP.NET server control's StateBag using either the Item property or the Add method. The StateBag then tracks changes to all the items that it stores. Any changes to a StateItem object is reflected in its IsDirty property. These changes are saved by a call to the SaveViewState method during the save view state phase of server control processing, just before the control is rendered to the page. For more information, see Control Execution Lifecycle.


[Visual Basic, C#, C++] The following example uses the Value and IsDirty properties of the StateItem class to save the state of a simple custom ASP.NET server control class, FirstCustomControl. When the page has posted to the server, the IsDirty property checks whether the item has been modified. The state values are displayed by accessing the Value property.

[Visual Basic] 
' Create a function that iterates through the view-state
' values stored for this class and returns the
' results as a string.
Public Function EnumerateViewState() As String
   Dim keyName, keyValue As String
   Dim result As String = [String].Empty
   Dim myStateItem As StateItem
   Dim myDictionaryEnumerator As IDictionaryEnumerator = _viewstate.GetEnumerator()
   While myDictionaryEnumerator.MoveNext()
      keyName = CStr(myDictionaryEnumerator.Key)
      myStateItem = CType(myDictionaryEnumerator.Value, StateItem)
      keyValue = CStr(myStateItem.Value)
      result = result + "<br>ViewState[" + keyName + "] = " + keyValue
   End While
   Return result
End Function 'EnumerateViewState
End Class 'MyItem 

// Create a function that iterates through the view-state
// values stored for this class and returns the
// results as a string.
public string EnumerateViewState()
   string keyName,keyValue;
   string result = String.Empty;
   StateItem myStateItem;
   IDictionaryEnumerator myDictionaryEnumerator = _viewstate.GetEnumerator();
      keyName = (string)myDictionaryEnumerator.Key;
      myStateItem = (StateItem)myDictionaryEnumerator.Value;
      keyValue = (string)myStateItem.Value;
      result = result + "<br>ViewState["  + keyName + "] = " + keyValue;
   return result;

// Create a function that iterates through the view-state
// values stored for this class and returns the
// results as a string.
String* EnumerateViewState() {
   String* keyName, * keyValue;
   String* result = String::Empty;
   StateItem* myStateItem;
   IDictionaryEnumerator* myDictionaryEnumerator = 
   while(myDictionaryEnumerator->MoveNext()) {
      keyName = dynamic_cast<String*>(myDictionaryEnumerator->Key);
      myStateItem = 
      keyValue = dynamic_cast<String*>(myStateItem->Value);
      result = 
         String::Concat(result, S"<br>ViewState->Item[ ", keyName, S"] = ", keyValue);
   return result;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

StateItem Members | System.Web.UI Namespace | StateBag | SaveViewState | Control Execution Lifecycle | Introduction to Web Forms State Management