asp.net.ph

HtmlControl.Attributes Property

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.

Syntax


Script HtmlControl.Attributes [ strAttribute ] = strValue

This property can only be used programmatically; it cannot be set when declaring the control.

Property Value

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.

Remarks

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.

Example

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 

See Also

HtmlControl Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph