Export (0) Print
Expand All

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.

System.Object
   System.Web.UI.StateItem

[Visual Basic]
NotInheritable Public Class StateItem
[C#]
public sealed class StateItem
[C++]
public __gc __sealed class StateItem
[JScript]
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.

Remarks

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.

Example

[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 

[C#] 
// 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();
   while(myDictionaryEnumerator.MoveNext())
   {
      keyName = (string)myDictionaryEnumerator.Key;
      myStateItem = (StateItem)myDictionaryEnumerator.Value;
      keyValue = (string)myStateItem.Value;
      result = result + "<br>ViewState["  + keyName + "] = " + keyValue;
   }
   return result;
}

[C++] 
// 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();
   while(myDictionaryEnumerator->MoveNext()) {
      keyName = dynamic_cast<String*>(myDictionaryEnumerator->Key);
      myStateItem = 
         dynamic_cast<StateItem*>(myDictionaryEnumerator->Value);
      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.

Requirements

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

Show:
© 2015 Microsoft