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.

For a list of all members of this type, see AttributeCollection Members.

System.Object
   System.Web.UI.AttributeCollection

[Visual Basic]
NotInheritable Public Class AttributeCollection
[C#]
public sealed class AttributeCollection
[C++]
public __gc __sealed class AttributeCollection
[JScript]
public class AttributeCollection

Thread Safety

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

Remarks

Attributes' case-sensitivity depends upon whether the StateBag class from which it inherits is case-sensitive or not. They return a String object as their value. If there are no attributes in the collection, they return a null reference (Nothing in Visual Basic).

Attributes on an HTML server control are programmatically accessible through the HtmlControl.Attributes property, which is inherited by all HTML server controls. The .NET Framework treats HTML attributes as properties of the HTML server control to which they belong.

You can also add attributes to a Web server control through the WebControl.Attributes property, which is inherited by all Web server controls. Attributes stored in this collection for a Web server control do not correspond to the strongly typed properties found on the specified Web server control.

Example

[Visual Basic, C#] The following example creates a new AttributeCollection, named myAttributeCollection, and then checks whether the page has been posted back. If it has not, it adds two attributes to the collection, 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 posted back, the sample simply gets the new number of attributes and iterates through the collection, writing the key to each attribute and its value to the page.

[Visual Basic] 
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

[C#] 
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++;
      }
   }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

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

See Also

AttributeCollection Members | System.Web.UI Namespace | WebControl.Attributes | HtmlControl.Attributes

Show:
© 2014 Microsoft