次の方法で共有


StateBag クラス

ページを含む、ASP.NET サーバー コントロールのビューステートを管理します。このクラスは継承できません。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public NotInheritable Class StateBag
    Implements IStateManager, IDictionary, ICollection, IEnumerable
'使用
Dim instance As StateBag
public sealed class StateBag : IStateManager, IDictionary, ICollection, 
    IEnumerable
public ref class StateBag sealed : IStateManager, IDictionary, ICollection, 
    IEnumerable
public final class StateBag implements IStateManager, IDictionary, 
    ICollection, IEnumerable
public final class StateBag implements IStateManager, IDictionary, 
    ICollection, IEnumerable
適用できません。

解説

ページまたはコントロールのビューステートは、そのページまたはコントロールの累積プロパティ値 (つまりビュー) によって表されます。このクラスにアクセスするには、Control.ViewState プロパティを使用します。また、コントロールは、基本的な状態情報をコントロールの状態に格納しますが、この情報は、StateBag オブジェクトとしては格納されません。

このクラスは、すべての HTML サーバー コントロールおよび Web サーバー コントロールの主要なストレージ機構です。このクラスは、属性と値のペアをコントロールに関連付けられた文字列として格納します。ページ要求に対して OnInit メソッドが実行されてから、これらの属性に対する変更を追跡し、ページまたはコントロールのビューステートへの変更を保存します。

このクラスにはディクショナリの一種が実装されているため、一般的なディクショナリ オブジェクトと同様に、ディクショナリの項目を追加したり削除したりできます。ディクショナリなどのデータ コレクションの詳細については、「コレクションとデータ構造体」を参照してください。

使用例

Text プロパティおよび FontSize プロパティを持つ複合 Label コントロールのコード例を次に示します。Control.Render メソッドがコントロールで呼び出されると、これらのプロパティはビューステートに保存されたり、ビューステートから取得されたりします。

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

.NET Framework のセキュリティ

継承階層

System.Object
  System.Web.UI.StateBag

スレッド セーフ

この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

StateBag メンバ
System.Web.UI 名前空間
IStateManager
IDictionary
Control.ViewState プロパティ
System.Web.UI.AttributeCollection

その他の技術情報

コレクションとデータ構造体
ASP.NET の状態管理の概要