System.Web.UI.WebControls Namespace WebControl Class
Returns the collection of attributes applied to a Web control. Also sets or retrieves the individual attribute items in the collection.
Script |
WebControl.Attributes [ strAttribute ] [ = strValue ] |
This property can only be used programmatically; it cannot be set when declaring the control.
A System.Web.UI.AttributeCollection of name and value pairs.
When accesing individual attributes in the collection:
strAttribute |
A string that denotes an attribute that is applicable to the given control. |
strValue |
A value of a type that is applicable to the given attribute, typically a string, numeric, or boolean value. |
This property is read/write.
The Attributes collection comprises the list of all attributes declared within the opening tag of a Web server control. This enables object-model access to each of the attributes defined for the control.
Like any collection in ASP.NET, the individual attribute items can be accessed using either the attribute's name or ordinal index ( the position of the item in the order in which the items have been added to the list ). And as with other collections, attributes may dynamically be added to or removed from the collection.
NOTE: This property returns all attributes defined for a given control, regardless of whether the current client browser supports the attribute or not.
This feature requires Microsoft® Internet Explorer® 4.0 or later.
The following example demonstrates how to enumerate the items of the AttributeCollection object retrieved from a Web control. Note that the attributes stored in this collecion reflect only the arbitrary properties that do not correspond to the properties defined in the .NET Framework for the control.
<script language = "C#" runat = "server">
string atts;
void showAttributes ( Object src, EventArgs e ) {
IEnumerator attribs = greeting.Attributes.Keys.GetEnumerator ( );
while ( attribs.MoveNext ( ) ) {
string key = ( string ) attribs.Current;
atts += key + " = " + greeting.Attributes [ key ] + "<br>";;
}
message.Text = atts;
}
</script>
Show me
The following example illustrates how the Attributes property of a Web control can be used to run a client-side script command when the TextBox control loses focus.
<html>
<head>
<script language = "C#" runat = "server">
void Page_Load ( Object src, EventArgs e ) {
TextBox1.Attributes [ "onBlur" ] = "javascript:alert" +
" ( 'This message will appear each time the TextBox loses focus.' ) ";
}
</script>
</head>
<body>
<form runat = "server">
<asp:TextBox id = "TextBox1" size=40 runat = "server"
Text = "Click here and then tap out of this text box" />
</form>
</body>
</html>
Show me
WebControl Members Base Web Control Properties