StateBag 类

定义

管理 ASP.NET 服务器控件(包括页面)的视图状态。 此类不能被继承。

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控件。 在控件上调用该方法时 Control.Render ,这些属性将保存到视图状态并从中检索。

// 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

注解

页面或控件的视图状态是该页或控件的累积属性值或视图。 可以通过属性访问此类 Control.ViewState 。 控件还可以将基本状态信息存储在控件状态中,但该信息不存储为 StateBag 对象。

此类是所有 HTML 和 Web 服务器控件的主存储机制。 它将特性/值对存储为与控件关联的字符串。 它仅在为页面请求执行方法后 OnInit 跟踪对这些属性的更改,并将更改保存到页面或控件的视图状态。

此类实现字典,你可以像任何字典对象一样向其添加项或删除项。 有关数据集合(如字典)的详细信息,请参阅 集合和数据结构

构造函数

StateBag()

初始化 StateBag 类的新实例。 这是该类的无参数构造函数。

StateBag(Boolean)

初始化 StateBag 类的新实例,该实例允许存储的状态值不区分大小写。

属性

Count

获取 StateItem 对象中 StateBag 对象的数目。

Item[String]

获取或设置在 StateBag 对象中存储的项的值。

Keys

获取表示 StateBag 对象中的项的键集合。

Values

获取存储在 StateBag 对象中的视图状态值的集合。

方法

Add(String, Object)

将新的 StateItem 对象添加到 StateBag 对象。 如果该项已经存在于 StateBag 对象中,则此方法会更新该项的值。

Clear()

从当前 StateBag 对象中移除所有项。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回循环访问 StateItem 对象中存储的 StateBag 对象的所有键/值对的枚举数。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IsItemDirty(String)

检查 StateItem 对象中存储的 StateBag 对象,以计算自调用 TrackViewState() 后该对象是否进行了修改。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Remove(String)

将指定的密钥/值对从 StateBag 对象中移除。

SetDirty(Boolean)

设置 StateBag 对象的状态以及每个内含 StateItem 对象的 Dirty 属性。

SetItemDirty(String, Boolean)

Dirty 对象中的指定 StateItem 对象设置 StateBag 属性。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

有关此成员的说明,请参见 CopyTo(Array, Int32)

ICollection.IsSynchronized

有关此成员的说明,请参见 IsSynchronized

ICollection.SyncRoot

有关此成员的说明,请参见 SyncRoot

IDictionary.Add(Object, Object)

有关此成员的说明,请参见 Add(Object, Object)

IDictionary.Contains(Object)

有关此成员的说明,请参见 Contains(Object)

IDictionary.IsFixedSize

有关此成员的说明,请参见 IsFixedSize

IDictionary.IsReadOnly

有关此成员的说明,请参见 IsReadOnly

IDictionary.Item[Object]

有关此成员的说明,请参见 Item[Object]

IDictionary.Remove(Object)

有关此成员的说明,请参见 Remove(Object)

IEnumerable.GetEnumerator()

有关此成员的说明,请参见 GetEnumerator()

IStateManager.IsTrackingViewState

获取一个值,该值指示是否正在跟踪状态更改。

IStateManager.LoadViewState(Object)

还原以前保存的 StateBag 对象的视图状态。

IStateManager.SaveViewState()

保存自页回发到服务器后,对 StateBag 对象的更改。

IStateManager.TrackViewState()

使 StateBag 对象跟踪其状态的变更,以便该对象可在请求中一直留存。

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

另请参阅