Manages the view state of ASP.NET server controls, including pages. This class cannot be inherited.
For a list of all members of this type, see StateBag Members.
System.Object
System.Web.UI.StateBag
[Visual Basic]
NotInheritable Public Class StateBag
Implements IStateManager, IDictionary, ICollection, IEnumerable
[C#]
public sealed class StateBag : IStateManager, IDictionary,
ICollection, IEnumerable
[C++]
public __gc __sealed class StateBag : public IStateManager,
IDictionary, ICollection, IEnumerable
[JScript]
public class StateBag implements IStateManager, IDictionary,
ICollection, IEnumerable
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 for a page or control is the cumulative propery values, or view, of that page or control. You can access this class through the Control.ViewState property.
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 save the changes to the page's or control's view state.
You can read from this class during any stage of the control processing lifecycle, but you should not write to it while the control is rendering.
This class implements a dictionary, and you can add or remove items from it as you would any dictionary object. For more information about data collections, such as dictionaries, see Grouping Data in Collections.
Example
The following 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.
[Visual Basic]
' 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 LabelVB : Inherits Control
' Add property values to view state with set;
' retrieve them from view state with get.
Public Property [Text] As String
Get
Return CStr(ViewState("Text"))
End Get
Set
ViewState("Text") = Value
End Set
End Property
Public Property FontSize As Integer
Get
Return CInt(ViewState("FontSize"))
End Get
Set
ViewState("FontSize") = Value
End Set
End Property
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub Render(Output As HtmlTextWriter)
Output.Write("<font size=" & Me.FontSize & ">" & Me.Text & "</font>")
End Sub
End Class
End Namespace
[C#]
// 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 Label: Control {
// Add property values to view state with set;
// retrieve them from view state with get.
public String Text {
get {
return (String) ViewState["Text"];
}
set {
ViewState["Text"] = value;
}
}
public int FontSize {
get {
return (int) ViewState["FontSize"];
}
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 + ">" + this.Text + "</font>");
}
}
}
[C++]
// This control renders values stored in view state for Text and FontSize properties.
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web;
using namespace System::Web::UI;
public __gc class Label : public Control
{
// Add property values to view state with set;
// retrieve them from view state with get.
public:
__property String * get_Text()
{
return dynamic_cast<String*>(ViewState->Item[S"Text"]);
}
__property void set_Text(String * value)
{
ViewState->Item[S"Text"] = value;
}
__property int get_FontSize()
{
return *dynamic_cast<__box int*>(ViewState->Item[S"FontSize"]);
}
__property void set_FontSize(int value)
{
ViewState->Item[S"FontSize"] = __box(value);
}
protected:
[System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
void Render(HtmlTextWriter * output)
{
output->Write(S"<font size= {0} {1} </font>", __box(this->FontSize), this->Text);
}
};
[JScript] Requirements
Namespace: System.Web.UI
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
Assembly: System.Web (in System.Web.dll)
See Also
StateBag Members | System.Web.UI Namespace | IStateManager | IDictionary | ViewState | Introduction to Web Forms State Management | Grouping Data in Collections | System.Web.UI.AttributeCollection