Export (0) Print
Expand All

WebControl.Attributes Property

Gets the collection of arbitrary attributes (for rendering only) that do not correspond to properties on the control.

[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 of name and value pairs.

Remarks

The Attributes collection contains a collection of all attributes declared in the opening tag of a Web server control. This allows you to programmatically control the attributes associated with a Web server control. You can add attributes to the collection or remove attributes from the collection.

Note   This property is rendered with all attributes in the collection in the controls opening tag, regardless of the browser settings. Not all browsers support every attribute that is rendered. The unsupported attributes are usually ignored by the browser.

Example

[Visual Basic, C#, JScript] The following example illustrates how the Attributes property of a WebControl can be used to run a JavaScript command when the TextBox control loses focus.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

 <html>
 <head>
    <script language="VB" runat="server">
 
        Sub Page_Load(sender As Object, e As EventArgs)
            TextBox1.Attributes("onblur") = "javascript:alert('Hello! Focus lost from text box!!');"
        End Sub
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form runat="server">
 
    <asp:TextBox id="TextBox1" columns=54 
     Text="Click here and then tap out of this text box" 
     runat="server"/>  
 
 </form>
 </body>
 </html>
 

[C#] 

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

 <html>
 <head>
    <script language="C#" runat="server">
 
        void Page_Load(Object sender, EventArgs e) {
           TextBox1.Attributes["onblur"]="javascript:alert('Hello! Focus lost from text box!!');";    
        }
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form runat="server">
 
    <asp:TextBox id="TextBox1" columns=54 
     Text="Click here and then tap out of this text box" 
     runat="server"/>  
 
 </form>
 </body>
 </html>
 

[JScript] 

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

 <html>
 <head>
    <script language="JSCRIPT" runat="server">
 
        function Page_Load(sender : Object, e : EventArgs){
            TextBox1.Attributes("onblur") = "javascript:alert('Focus lost from text box!!');"
        }
    </script>
 
 </head>
 <body>
    <h3>Attributes Property of a Web Control</h3>
 <form runat="server">
 
    <asp:TextBox id="TextBox1" columns=54 
     Text="Click here and then tab out of this text box" 
     runat="server"/>  
 
 </form>
 </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 2000, Windows XP Professional, Windows Server 2003 family

See Also

WebControl Class | WebControl Members | System.Web.UI.WebControls Namespace | System.Web.UI.AttributeCollection

Show:
© 2014 Microsoft