AttributeCollection Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

System.Object
  System.Web.UI.AttributeCollection

Public NotInheritable Class AttributeCollection

NameDescription
System_CAPS_pubmethodAttributeCollection(StateBag)

Initializes a new instance of the AttributeCollection class.

NameDescription
System_CAPS_pubpropertyCount

Gets the number of attributes in the AttributeCollection object.

System_CAPS_pubpropertyCssStyle

Gets a collection of styles for the ASP.NET server control to which the current AttributeCollection object belongs.

System_CAPS_pubpropertyItem(String)

Gets or sets a specified attribute value for a server control.

System_CAPS_pubpropertyKeys

Gets a collection of keys to all attributes in the server control's AttributeCollection object.

NameDescription
System_CAPS_pubmethodAdd(String, String)

Adds an attribute to a server control's AttributeCollection object.

System_CAPS_pubmethodAddAttributes(HtmlTextWriter)

Adds attributes from the AttributeCollection class to the HtmlTextWriter object that is responsible for rendering the attributes as markup.

System_CAPS_pubmethodClear()

Removes all attributes from a server control's AttributeCollection object.

System_CAPS_pubmethodEquals(Object)

Determines whether the current instance of the AttributeCollection object is equal to the specified object.(Overrides Object.Equals(Object).)

System_CAPS_pubmethodGetHashCode()

Returns the hash code for this instance.(Overrides Object.GetHashCode().)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodRemove(String)

Removes an attribute from a server control's AttributeCollection object.

System_CAPS_pubmethodRender(HtmlTextWriter)

Writes the collection of attributes to the specified HtmlTextWriter output stream for the control to which the collection belongs.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

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.

  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

.NET Framework
Available since 1.1

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

Return to top
Show: