Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Attributes Property

HtmlControl.Attributes Property

Gets a collection of all attribute name and value pairs expressed on a server control tag within the .aspx file.

[Visual Basic]
Public ReadOnly Property Attributes As AttributeCollection
[C#]
public AttributeCollection Attributes {get;}
[C++]
public: __property AttributeCollection* get_Attributes();
[JScript]
public function get Attributes() : AttributeCollection;

Property Value

A System.Web.UI.AttributeCollection object that contains all attribute name and value pairs expressed on a server control tag within the Web page.

Remarks

Use this property to programmatically access the attributes of the HTML server control. All Html server controls store their attributes in the Control.ViewState.

HTML attributes are treated by the .NET Framework as properties on the HTML server control to which they belong.

For additional information on the attributes collection, see the System.Web.UI.AttributeCollection class.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to use the Attributes property to determine the attributes of the HtmlSelect control.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>

<script language="VB" runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
        Message.InnerHtml = "<h4>" & "The select box's attributes collection contains:" & "</h4>"
        
        Dim keys As IEnumerator = Select1.Attributes.Keys.GetEnumerator()
        
        While keys.MoveNext()
            
            Dim key As String = CType(keys.Current, String)
            Message.InnerHtml &= key & "=" & Select1.Attributes(key) & "<br>"
        End While 
    End Sub 'Page_Load

</script>

<body>

   <h3>HtmlControl Attribute Collection Example</h3>

   Make a selection:

   <select id="Select1" 
           style="font: 12pt verdana;
                 background-color:yellow;
                 color:red;" 
           runat="server">

      <option>Item 1</option>
      <option>Item 2</option>
      <option>Item 3</option>

   </select>

   <p>

   <span id="Message" MaintainState="false" runat="server" />
   

</body>
</html>
   

[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>

<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e) 
   {
      Message.InnerHtml = "<h4>The select box's attributes collection contains:</h4>";
     
      IEnumerator keys = Select.Attributes.Keys.GetEnumerator();

      while (keys.MoveNext()) 
      {

         String key = (String)keys.Current;
         Message.InnerHtml += key + "=" + Select.Attributes[key] + "<br>";

      }
   }

</script>

<body>

   <h3>HtmlControl Attribute Collection Example</h3>

   Make a selection:

   <select id="Select" 
           style="font: 12pt verdana;
                 background-color:yellow;
                 color:red;" 
           runat="server">

      <option>Item 1</option>
      <option>Item 2</option>
      <option>Item 3</option>

   </select>

   <p>

   <span id="Message" MaintainState="false" runat="server" />
   

</body>
</html>
   

[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>

<html>

<script language="jscript" runat="server">
    function Page_Load(sender: Object, e: EventArgs){
        Message.InnerHtml = "<h4>The select box's attributes collection contains:</h4>"
        
        var keys: IEnumerator = Select1.Attributes.Keys.GetEnumerator()
        
        while(keys.MoveNext()){
            var key: String = String(keys.Current)
            Message.InnerHtml += key + "=" + Select1.Attributes(key) + "<br>"
        }
    }

</script>

<body>

   <h3>HtmlControl Attribute Collection Example</h3>

   Make a selection:

   <select id="Select1" 
           style="font: 12pt verdana;
                 background-color:yellow;
                 color:red;" 
           runat="server">

      <option>Item 1</option>
      <option>Item 2</option>
      <option>Item 3</option>

   </select>

   <p>

   <span id="Message" MaintainState="false" runat="server" />
   

</body>
</html>
   

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlControl Class | HtmlControl Members | System.Web.UI.HtmlControls Namespace | System.Web.UI.AttributeCollection | Control.ViewState | HtmlSelect

Show:
© 2015 Microsoft