AttributeCollection 類別

定義

提供物件模型 (Object Model) 的存取權給 ASP.NET 伺服器控制項項目的開頭標記中宣告的所有屬性。Provides object-model access to all attributes declared in the opening tag of an ASP.NET server control element. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列範例會建立名AttributeCollection myAttributeCollection為的新物件, 然後檢查是否已回傳該頁面。The following example creates a new AttributeCollection object that is named myAttributeCollection, and then checks whether the page has been posted back. 如果沒有, 則程式碼會將兩個屬性新增至集合。If it has not, the code adds two attributes to the collection. 然後, 它會取得集合中的屬性數目, 並逐一查看集合, 將索引鍵寫入頁面的每個屬性。It then gets the number of attributes in the collection and iterates through the collection, writing the key to each attribute to the page. 如果頁面是回傳, 則程式碼會取得新的屬性數目, 並逐一查看集合, 並將每個屬性的索引鍵和值寫入頁面。If the page is a postback, the code gets the new number of attributes and iterates through the collection, writing the key and value of each attribute to the page.

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物件作為其值。Individual items in the collection return a String object as their value. 如果集合中沒有屬性專案, 集合null就會傳回。If there are no attribute items in the collection, the collection returns null.

Html 伺服器控制項上的屬性是透過Attributes以程式設計方式提供的屬性, 由所有 HTML 伺服器控制項繼承。Attributes on an HTML server control are programmatically available through the Attributes property, which is inherited by all HTML server controls. ASP.NET 會公開 HTML 伺服器控制項的屬性, 做為這些控制項的屬性。ASP.NET exposes attributes of HTML server controls as properties of those controls.

您可以透過Attributes屬性 (由所有 Web 服務器控制項繼承), 將屬性加入至 Web 服務器控制項。You can add attributes to a Web server control through the Attributes property, which is inherited by all Web server controls. Web 服務器控制項的屬性集合中的屬性不一定會對應至控制項的強型別屬性 (attribute)。The attributes in the attributes collection for a Web server control do not necessarily correspond to the control's strongly typed properties for that control.

建構函式

AttributeCollection(StateBag)

初始化 AttributeCollection 類別的新執行個體。Initializes a new instance of the AttributeCollection class.

屬性

Count

取得 AttributeCollection 物件中的屬性數目。Gets the number of attributes in the AttributeCollection object.

CssStyle

取得目前 AttributeCollection 物件所屬的 ASP.NET 伺服器控制項樣式的集合。Gets a collection of styles for the ASP.NET server control to which the current AttributeCollection object belongs.

Item[String]

取得或設定伺服器控制項的指定屬性值。Gets or sets a specified attribute value for a server control.

Keys

取得伺服器控制項的 AttributeCollection 物件中的所有屬性的索引鍵集合。Gets a collection of keys to all attributes in the server control's AttributeCollection object.

方法

Add(String, String)

將屬性加入至伺服器控制項的 AttributeCollection 物件。Adds an attribute to a server control's AttributeCollection object.

AddAttributes(HtmlTextWriter)

將屬性從 AttributeCollection 類別加入負責將屬性當做標記呈現的 HtmlTextWriter 物件。Adds attributes from the AttributeCollection class to the HtmlTextWriter object that is responsible for rendering the attributes as markup.

Clear()

從伺服器控制項的 AttributeCollection 物件中移除所有屬性。Removes all attributes from a server control's AttributeCollection object.

Equals(Object)

判斷 AttributeCollection 物件目前的執行個體是否等於指定的物件。Determines whether the current instance of the AttributeCollection object is equal to the specified object.

GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

GetType()

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

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Remove(String)

從伺服器控制項的 AttributeCollection 物件移除屬性。Removes an attribute from a server control's AttributeCollection object.

Render(HtmlTextWriter)

將屬性集合寫入為集合所屬控制項所指定的 HtmlTextWriter 輸出資料流。Writes the collection of attributes to the specified HtmlTextWriter output stream for the control to which the collection belongs.

ToString()

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

(繼承來源 Object)

適用於

另請參閱