StateBag 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
管理 ASP.NET 服务器控件(包括页面)的视图状态。 此类不能被继承。
public ref class StateBag sealed : System::Collections::IDictionary, System::Web::UI::IStateManagerpublic sealed class StateBag : System.Collections.IDictionary, System.Web.UI.IStateManagertype StateBag = class
    interface IStateManager
    interface IDictionary
    interface ICollection
    interface IEnumerablePublic NotInheritable Class StateBag
Implements IDictionary, IStateManager- 继承
- 
				StateBag
- 实现
示例
下面的代码示例演示具有 Text 和 FontSize 属性的复合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 | |
| Item[String] | 获取或设置在 StateBag 对象中存储的项的值。 | 
| Keys | 获取表示 StateBag 对象中的项的键集合。 | 
| Values | 获取存储在 StateBag 对象中的视图状态值的集合。 | 
方法
| Add(String, Object) | 将新的 StateItem 对象添加到 StateBag 对象。 如果该项已经存在于 StateBag 对象中,则此方法会更新该项的值。 | 
| Clear() | 从当前 StateBag 对象中移除所有项。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | |
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsItemDirty(String) | 检查 StateItem 对象中存储的 StateBag 对象,以计算自调用 TrackViewState() 后该对象是否进行了修改。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Remove(String) | 将指定的密钥/值对从 StateBag 对象中移除。 | 
| SetDirty(Boolean) | |
| SetItemDirty(String, Boolean) | |
| 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。 |