Export (0) Print
Expand All

AttributeCollection Class

Provides object-model access to all attributes declared in the opening tag of an ASP.NET server control element. This class cannot be inherited.

System.Object
  System.Web.UI.AttributeCollection

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class AttributeCollection

The AttributeCollection type exposes the following members.

  NameDescription
Public methodAttributeCollectionInitializes a new instance of the AttributeCollection class.
Top

  NameDescription
Public propertyCountGets the number of attributes in the AttributeCollection object.
Public propertyCssStyleGets a collection of styles for the ASP.NET server control to which the current AttributeCollection object belongs.
Public propertyItemGets or sets a specified attribute value for a server control.
Public propertyKeysGets a collection of keys to all attributes in the server control's AttributeCollection object.
Top

  NameDescription
Public methodAddAdds an attribute to a server control's AttributeCollection object.
Public methodAddAttributesAdds attributes from the AttributeCollection class to the HtmlTextWriter object that is responsible for rendering the attributes as markup.
Public methodClearRemoves all attributes from a server control's AttributeCollection object.
Public methodEqualsDetermines whether the current instance of the AttributeCollection object is equal to the specified object. (Overrides Object.Equals(Object).)
Public methodGetHashCodeReturns the hash code for this instance. (Overrides Object.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodRemoveRemoves an attribute from a server control's AttributeCollection object.
Public methodRenderWrites the collection of attributes to the specified HtmlTextWriter output stream for the control to which the collection belongs.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Individual items in the collection return a String object as their value. If there are no attribute items in the collection, the collection returns null.

Attributes on an HTML server control are programmatically available through the Attributes property, which is inherited by all HTML server controls. ASP.NET exposes attributes of HTML server controls as properties of those controls.

You can add attributes to a Web server control through the Attributes property, which is inherited by all Web server controls. 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.

TopicLocation
How to: Read HTML Attributes for Controls in Web Forms PagesBuilding ASP .NET Web Applications
How to: Read HTML Attributes for Controls in Web Forms PagesBuilding ASP .NET Web Applications

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++;
      }
   }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft