StateBag 类

定义

管理 ASP.NET 服务器控件(包括页)的视图状态。Manages the view state of ASP.NET server controls, including pages. 此类不能被继承。This class cannot be inherited.

public ref class StateBag sealed : System::Collections::IDictionary, System::Web::UI::IStateManager
public sealed class StateBag : System.Collections.IDictionary, System.Web.UI.IStateManager
type StateBag = class
    interface IStateManager
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public NotInheritable Class StateBag
Implements IDictionary, IStateManager
继承
StateBag
实现

示例

下面的代码示例演示一个具有 TextFontSize 属性的组合 Label 控件。The following code example demonstrates a composite Label control that has Text and FontSize properties. 当对控件调用 Control.Render 方法时,这些属性将保存到视图状态,并从视图状态检索。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>");
        }
    }
}
' This control renders values stored in view state for Text and FontSize properties.

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

注解

页面或控件的视图状态是该页面或控件的累计属性值或视图。View state for a page or control is the cumulative property values, or view, of that page or control. 可以通过 Control.ViewState 属性访问此类。You can access this class through the Control.ViewState property. 控件还可以在控件状态中存储基本状态信息,但这些信息并不存储为 StateBag 对象。Controls can also store essential state information in control state, but that information is not stored as a StateBag object.

此类是所有 HTML 和 Web 服务器控件的主要存储机制。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. 仅在对页面请求执行 OnInit 方法后,它才跟踪对这些属性的更改,并将更改保存到页面或控件的视图状态。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.

构造函数

StateBag()

初始化 StateBag 类的新实例。Initializes a new instance of the StateBag class. 这是该类的无参数构造函数。This is the parameterless constructor for this class.

StateBag(Boolean)

初始化 StateBag 类的新实例,该实例允许存储的状态值不区分大小写。Initializes a new instance of the StateBag class that allows stored state values to be case-insensitive.

属性

Count

获取 StateBag 对象中的 StateItem 对象数。Gets the number of StateItem objects in the StateBag object.

Item[String]

获取或设置在 StateBag 对象中存储的项的值。Gets or sets the value of an item stored in the StateBag object.

Keys

获取表示 StateBag 对象中的项的键集合。Gets a collection of keys representing the items in the StateBag object.

Values

获取存储在 StateBag 对象中的视图状态值的集合。Gets a collection of the view-state values stored in the StateBag object.

方法

Add(String, Object)

将新的 StateItem 对象添加到 StateBag 对象。Adds a new StateItem object to the StateBag object. 如果该项已经存在于 StateBag 对象中,则此方法会更新该项的值。If the item already exists in the StateBag object, this method updates the value of the item.

Clear()

从当前 StateBag 对象中移除所有项。Removes all items from the current StateBag object.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetEnumerator()

返回循环访问 StateItem 对象中存储的 StateBag 对象的所有键/值对的枚举数。Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsItemDirty(String)

检查 StateItem 对象中存储的 StateBag 对象,以计算自调用 TrackViewState() 后该对象是否进行了修改。Checks a StateItem object stored in the StateBag object to evaluate whether it has been modified since the call to TrackViewState().

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Remove(String)

将指定的密钥/值对从 StateBag 对象中移除。Removes the specified key/value pair from the StateBag object.

SetDirty(Boolean)

设置 StateBag 对象的状态以及每个内含 StateItem 对象的 Dirty 属性。Sets the state of the StateBag object as well as the Dirty property of each of the StateItem objects contained by it.

SetItemDirty(String, Boolean)

Dirty 对象中的指定 StateItem 对象设置 StateBag 属性。Sets the Dirty property for the specified StateItem object in the StateBag object.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

ICollection.CopyTo(Array, Int32)

有关此成员的说明,请参见 CopyTo(Array, Int32)For a description of this member, see CopyTo(Array, Int32).

ICollection.IsSynchronized

有关此成员的说明,请参见 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot

有关此成员的说明,请参见 SyncRootFor a description of this member, see SyncRoot.

IDictionary.Add(Object, Object)

有关此成员的说明,请参见 Add(Object, Object)For a description of this member, see Add(Object, Object).

IDictionary.Contains(Object)

有关此成员的说明,请参见 Contains(Object)For a description of this member, see Contains(Object).

IDictionary.IsFixedSize

有关此成员的说明,请参见 IsFixedSizeFor a description of this member, see IsFixedSize.

IDictionary.IsReadOnly

有关此成员的说明,请参见 IsReadOnlyFor a description of this member, see IsReadOnly.

IDictionary.Item[Object]

有关此成员的说明,请参见 Item[Object]For a description of this member, see Item[Object].

IDictionary.Remove(Object)

有关此成员的说明,请参见 Remove(Object)For a description of this member, see Remove(Object).

IEnumerable.GetEnumerator()

有关此成员的说明,请参见 GetEnumerator()For a description of this member, see GetEnumerator().

IStateManager.IsTrackingViewState

获取一个值,该值指示是否正在跟踪状态更改。Gets a value indicating whether state changes are being tracked.

IStateManager.LoadViewState(Object)

还原以前保存的 StateBag 对象的视图状态。Restores the previously saved view state of the StateBag object.

IStateManager.SaveViewState()

保存自页回发到服务器后,对 StateBag 对象的更改。Saves the changes to the StateBag object since the time the page was posted back to the server.

IStateManager.TrackViewState()

使 StateBag 对象跟踪其状态的变更,以便该对象可在请求中一直留存。Causes the StateBag object to track changes to its state so that it can be persisted across requests.

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅