AttributeCollection クラス

定義

ASP.NET サーバー コントロール要素の開始タグで宣言されたすべての属性へのオブジェクト モデル アクセスを提供します。 このクラスは継承できません。

public ref class AttributeCollection sealed
public sealed class AttributeCollection
type AttributeCollection = class
Public NotInheritable Class AttributeCollection
継承
AttributeCollection

次の例では、 という名前myAttributeCollectionの新しいAttributeCollectionオブジェクトを作成し、ページがポストバックされているかどうかを確認します。 そうでない場合、コードは 2 つの属性をコレクションに追加します。 次に、コレクション内の属性の数を取得し、コレクションを反復処理して、各属性にキーをページに書き込みます。 ページがポストバックの場合、コードは新しい数の属性を取得し、コレクションを反復処理して、各属性のキーと値をページに書き込みます。

AttributeCollection myAttributeCollection = null;

void Page_Load(object sender,EventArgs e)
{
   myAttributeCollection = new AttributeCollection(ViewState);
   Response.Write("<h3> AttributeCollection.AttributeCollection Sample </h3>");
   if (!IsPostBack)
   {  
      myAttributeCollection.Add("Color" ,"Color.Red");
      myAttributeCollection.Add("BackColor","Color.blue");
      Response.Write("Attribute Collection  count before PostBack = " + myAttributeCollection.Count);
      Response.Write("<br /><u><h4>Enumerating Attributes for CustomControl before PostBack</h4></u>");
      IEnumerator keys = myAttributeCollection.Keys.GetEnumerator();
      int i =1;
      String key;
      while (keys.MoveNext())
      {
         key = (String)keys.Current;
         Response.Write(i + ". "+key + "=" + myAttributeCollection[key]+"<br />");
         i++;
      }
   }
   else
   {
      Response.Write("Attribute Collection  count after PostBack = "+myAttributeCollection.Count);
      Response.Write("<br /><u><h4>Enumerating Attributes for CustomControl after PostBack</h4></u>");
      IEnumerator keys = myAttributeCollection.Keys.GetEnumerator();
      int i =1;
      String key;
      while (keys.MoveNext())
      {
         key = (String)keys.Current;
         Response.Write(i + ". "+key + "=" + myAttributeCollection[key]+"<br />");
         i++;
      }
   }
}
  Dim myAttributeCollection As AttributeCollection = Nothing 

  Sub Page_Load(sender As Object, e As EventArgs)
      myAttributeCollection = New AttributeCollection(ViewState)
      Response.Write("<h3> AttributeCollection.AttributeCollection Sample </h3>")
      If Not IsPostBack Then
         myAttributeCollection.Add("Color", "Color.Red")
         myAttributeCollection.Add("BackColor", "Color.blue")
         Response.Write("Attribute Collection count before PostBack = " & _
myAttributeCollection.Count.ToString())
         Response.Write("<br /><u><h4>Enumerating Attributes for " & _
                                 "CustomControl before PostBack</h4></u>")
         Dim keys As IEnumerator = myAttributeCollection.Keys.GetEnumerator()
         Dim i As Integer = 1
         Dim key As String
         While keys.MoveNext()
            key = CType(keys.Current, String)
            Response.Write(i.ToString() + ". " + key + "=" + myAttributeCollection(key) + "<br />")
            i += 1
         End While
      Else
         Response.Write("Attribute Collection  count after PostBack = " + _
                                    myAttributeCollection.Count.ToString())
         Response.Write("<br /><u><h4>Enumerating Attributes for " + _
                                 "CustomControl after PostBack</h4></u>")
         Dim keys As IEnumerator = myAttributeCollection.Keys.GetEnumerator()
         Dim i As Integer = 1
         Dim key As String
         While keys.MoveNext()
            key = CType(keys.Current, String)
            Response.Write(i.ToString() + ". " + key + "=" + myAttributeCollection(key) + "<br />")
            i += 1
         End While
      End If
   End Sub

注釈

コレクション内の個々の項目は、オブジェクトを String 値として返します。 コレクションに属性項目がない場合、コレクションは を返します null

HTML サーバー コントロールの属性は、 プロパティを通じて Attributes プログラムで使用できます。このプロパティは、すべての HTML サーバー コントロールによって継承されます。 ASP.NET は、HTML サーバー コントロールの属性をこれらのコントロールのプロパティとして公開します。

属性を Web サーバー コントロールに追加するには、 プロパティを Attributes 使用します。このプロパティは、すべての Web サーバー コントロールによって継承されます。 Web サーバー コントロールの attributes コレクション内の属性は、必ずしもそのコントロールの厳密に型指定されたプロパティに対応しているとは限りません。

コンストラクター

AttributeCollection(StateBag)

AttributeCollection クラスの新しいインスタンスを初期化します。

プロパティ

Count

AttributeCollection オブジェクト内の属性の数を取得します。

CssStyle

現在の AttributeCollection オブジェクトが属する ASP.NET サーバー コントロールのスタイルのコレクションを取得します。

Item[String]

サーバー コントロールに指定されている属性値を取得または設定します。

Keys

サーバー コントロールの AttributeCollection オブジェクトに格納されているすべての属性に対するキーのコレクションを取得します。

メソッド

Add(String, String)

サーバー コントロールの AttributeCollection オブジェクトに属性を追加します。

AddAttributes(HtmlTextWriter)

属性をマークアップとして表示する AttributeCollection オブジェクトに HtmlTextWriter クラスの属性を追加します。

Clear()

サーバー コントロールの AttributeCollection オブジェクトからすべての属性を削除します。

Equals(Object)

AttributeCollection オブジェクトの現在のインスタンスが指定したオブジェクトに等しいかどうかを判断します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

このインスタンスのハッシュ コードを返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
Remove(String)

サーバー コントロールの AttributeCollection オブジェクトから属性を削除します。

Render(HtmlTextWriter)

コレクションが属しているコントロールに指定した HtmlTextWriter 出力ストリームに、属性のコレクションを書き込みます。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください