System.Web.UI.HtmlControls HtmlControl Class
Returns the collection of attributes applied to an HtmlControl. Also sets or retrieves the individual attribute items in the collection.
Script |
HtmlControl.Attributes [ strAttribute ] = strValue |
This property can only be used programmatically; it cannot be set when declaring the control.
Returns a System.Web.UI.AttributeCollection of name and value pairs.
When accesing individual attributes in the collection:
strAttribute |
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 with no default value.
The Attributes collection comprises the list of all attributes declared within the opening tag of an HTML 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 below code shows how to enumerate the items of the AttributeCollection object retrieved from an HtmlControl. 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 an HtmlControl can be used to run a client-side script command when the HtmlInputText control loses focus.
<html>
<head>
<script language="C#" runat="server">
void Page_Load ( object src, EventArgs e ) {
myTextBox.Attributes [ "onBlur" ] = "javascript:alert" +
" ( 'This message will appear each time the TextBox loses focus.' ) ";
}
</script>
</head>
<body>
<form runat="server">
<input type=text id="myTextBox" size=35 runat="server"
value="Click here and then tap out of this text box" />
</form>
</body>
</html>
Show me
HtmlControl Members