Attributes Property
.NET Framework Class Library
WebControl..::.Attributes Property

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

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public ReadOnly Property Attributes As AttributeCollection
Visual Basic (Usage)
Dim instance As WebControl
Dim value As AttributeCollection

value = instance.Attributes
C#
[BrowsableAttribute(false)]
public AttributeCollection Attributes { get; }
Visual C++
[BrowsableAttribute(false)]
public:
property AttributeCollection^ Attributes {
    AttributeCollection^ get ();
}
JScript
public function get Attributes () : AttributeCollection

Property Value

Type: System.Web.UI..::.AttributeCollection
A AttributeCollection of name and value pairs.

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.

NoteNote:

This property is rendered with all attributes in the collection in the control’s 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.

NoteNote:

You cannot add client-side script to a WebControl instance using the Attributes collection. To add client-side script, use the ClientScript property on the Page control.

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.

NoteNote:

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

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

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>Attributes Property of a Web Control</title>
<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 id="form1" 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" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>Attributes Property of a Web Control</title>
<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 id="form1" 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" %>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  >
 <head>
    <title>Attributes Property of a Web Control</title>
<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 id="form1" runat="server">

    <asp:TextBox id="TextBox1" columns="54" 
     Text="Click here and then tab out of this text box" 
     runat="server"/>  

 </form>
 </body>
 </html>


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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
button.text      marta.billie   |   Edit   |   Show History
how to write a multi-lines text in a button?
Processing
Page view tracker