StateBag StateBag StateBag StateBag Class

定義

管理 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
繼承
StateBagStateBagStateBagStateBag
實作

範例

下列程式碼範例示範具有Label TextFontSize屬性的複合控制項。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
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)。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() StateBag() StateBag()

初始化 StateBag 類別的新執行個體。Initializes a new instance of the StateBag class. 這是此類別的無參數建構函式。This is the parameterless constructor for this class.

StateBag(Boolean) StateBag(Boolean) StateBag(Boolean) StateBag(Boolean)

初始化 StateBag 類別的新執行個體,允許儲存的狀態值不區分大小寫。Initializes a new instance of the StateBag class that allows stored state values to be case-insensitive.

屬性

Count Count Count Count

取得 StateItem 物件中的 StateBag 物件數目。Gets the number of StateItem objects in the StateBag object.

Item[String] Item[String] Item[String] Item[String]

取得或設定儲存於 StateBag 物件中的項目值。Gets or sets the value of an item stored in the StateBag object.

Keys Keys Keys Keys

取得表示 StateBag 物件中項目的索引鍵集合。Gets a collection of keys representing the items in the StateBag object.

Values Values Values Values

取得儲存於 StateBag 物件中的檢視狀態值的集合。Gets a collection of the view-state values stored in the StateBag object.

方法

Add(String, Object) Add(String, Object) Add(String, 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() Clear() Clear() Clear()

從目前 StateBag 物件移除所有項目。Removes all items from the current StateBag object.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

傳回列舉值,它可以逐一查看儲存在 StateItem 物件中之 StateBag 物件的所有索引鍵/值組。Returns an enumerator that iterates over all the key/value pairs of the StateItem objects stored in the StateBag object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsItemDirty(String) IsItemDirty(String) IsItemDirty(String) 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() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String) Remove(String) Remove(String) Remove(String)

StateBag 物件移除指定的索引鍵/數值配對。Removes the specified key/value pair from the StateBag object.

SetDirty(Boolean) SetDirty(Boolean) SetDirty(Boolean) 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) SetItemDirty(String, Boolean) SetItemDirty(String, Boolean) SetItemDirty(String, Boolean)

設定 Dirty 物件中指定 StateItem 物件的 StateBag 屬性。Sets the Dirty property for the specified StateItem object in the StateBag object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

如需這個成員的說明,請參閱 CopyTo(Array, Int32)For a description of this member, see CopyTo(Array, Int32).

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

如需這個成員的說明,請參閱 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

如需這個成員的說明,請參閱 SyncRootFor a description of this member, see SyncRoot.

IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object)

如需這個成員的說明,請參閱 Add(Object, Object)For a description of this member, see Add(Object, Object).

IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object)

如需這個成員的說明,請參閱 Contains(Object)For a description of this member, see Contains(Object).

IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize

如需這個成員的說明,請參閱 IsFixedSizeFor a description of this member, see IsFixedSize.

IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly

如需這個成員的說明,請參閱 IsReadOnlyFor a description of this member, see IsReadOnly.

IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object]

如需這個成員的說明,請參閱 Item[Object]For a description of this member, see Item[Object].

IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object)

如需這個成員的說明,請參閱 Remove(Object)For a description of this member, see Remove(Object).

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

如需這個成員的說明,請參閱 GetEnumerator()For a description of this member, see GetEnumerator().

IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState IStateManager.IsTrackingViewState

取得指出是否已追蹤狀態變更的值。Gets a value indicating whether state changes are being tracked.

IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object) IStateManager.LoadViewState(Object)

還原先前已儲存的 StateBag 物件檢視狀態。Restores the previously saved view state of the StateBag object.

IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState() IStateManager.SaveViewState()

儲存自頁面回傳至伺服器以來 StateBag 物件的變更。Saves the changes to the StateBag object since the time the page was posted back to the server.

IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState() IStateManager.TrackViewState()

導致 StateBag 物件追蹤其狀態的變更,以便可以跨要求持續保存。Causes the StateBag object to track changes to its state so that it can be persisted across requests.

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱