System.Web.UI Namespace


.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class StateBag _
    Implements IStateManager, IDictionary, ICollection, IEnumerable
Visual Basic (Usage)
Dim instance As StateBag
C#
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class StateBag : IStateManager, 
    IDictionary, ICollection, IEnumerable
Visual C++
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class StateBag sealed : IStateManager, 
    IDictionary, ICollection, IEnumerable
JScript
public final class StateBag implements IStateManager, IDictionary, ICollection, IEnumerable
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 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.

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 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 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
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 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>");
        }
    }
}
JScript
// This control renders values stored in view state for Text and FontSize properties.
import System
import System.Web
import System.Web.UI

package ViewStateControlSamples {

    public class Label extends Control {


       // Add property values to view state with set;
       // retrieve them from view state with get.
       public function get Text() : String 
       {
              return String(ViewState["Text"]);
       }

       public function set Text(value : String)
       {
              ViewState["Text"] = value;
       }


       public function get FontSize() : int 
       {
              return int(ViewState["FontSize"]);
       }

       public function set FontSize(value : int)
       {
              ViewState["FontSize"] = value;
       }

       protected override function Render(output : HtmlTextWriter) {
           output.Write("<font size=" + this.FontSize + ">" + this.Text + "</font>");
       }
    }    
}
.NET Framework Security

Inheritance Hierarchy

System..::.Object
  System.Web.UI..::.StateBag
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.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker