Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

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)
public sealed class StateBag : IStateManager, IDictionary, ICollection, 
	IEnumerable
public final class StateBag implements IStateManager, IDictionary, 
	ICollection, IEnumerable
public final class StateBag implements IStateManager, IDictionary, 
	ICollection, IEnumerable
Not applicable.

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.

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.

// 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>");
        }
    }
}

package ViewStateControlSamples;
// This control renders values stored in view state for Text and
// FontSize properties.
import System.*;
import System.Web.*;
import System.Web.UI.*;

public class Label extends Control
{
    // Add property values to view state with set;
    // retrieve them from view state with get.
    /** @property 
     */
    public String get_Text()
    {
        return ((String)(get_ViewState().get_Item("Text")));
    } //get_Text

    /** @property 
     */
    public void set_Text(String value)
    {
        get_ViewState().set_Item("Text", value);
    } //set_Text
    /** @property 
     */
    public int get_FontSize()
    {
        return (int)Convert.ToInt32(get_ViewState().get_Item("FontSize"));
    } //get_FontSize

    /** @property 
     */
    public void set_FontSize(int value)
    {
        get_ViewState().set_Item("FontSize", (Int32)value);
    } //set_FontSize

    protected void Render(HtmlTextWriter output)
    {
        output.Write("<font size=" + this.get_FontSize() + ">"
            + this.get_Text() + "</font>");
    } //Render
} //Label

// 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>");
       }
    }    
}

System.Object
  System.Web.UI.StateBag
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.