AttributeCollection 类

定义

提供对 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. 如果集合中没有特性项, 则集合返回nullIf 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 服务器控件的特性集合中的特性不一定对应于该控件的强类型属性。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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅