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.
Assembly: System.Web (in System.Web.dll)
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.
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++; } } }
-
AspNetHostingPermission
for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.