asp.net.ph

Skip Navigation Links

Setting HTML Server Control Properties Programmatically

ASP.NET Web Forms   Web Forms Server Controls   Programming Web Forms Server Controls


HTML server controls are of two slightly different types.

  • All the standard HTML form input elements, such as HtmlInputText, HtmlInputButton, HtmlSelect, etc., are available as individual HTML server controls. These HTML server controls expose their own, control-specific properties which map directly to HTML attributes.
  • In addition, any other HTML element can be converted to a control. In this case, the element becomes an HtmlGenericControl with base class properties such as TagName, Visible, and InnerHTML.

To set properties of HTML server controls

  • Get or set the property name as you would with any object. All properties are either strings or integers. The following examples illustrate this:

myAnchor.HRef = "http://aspalliance.com";
myTextBox.MaxLength = 20;
myTextBox.Value = string.Format ( "{0:$####}", TotalCost );
mySpan.InnerHtml = "You must enter a value for Email Address.";
  C# VB

All HTML server controls also support an Attributes collection, which gives you direct access to all the control’s attributes. This is particularly useful for working with attributes that are not exposed as individual properties.

To work with control attributes directly

  • Use the properties and methods of a control’s Attributes collection, such as Add, Remove, Clear, and Count. The Keys property returns a collection containing the names of all the attributes in the control. The following examples show various ways to use the Attributes collection:

// Adds a new attribute.
myTextBox.Attributes.Add ( "bgcolor","red" );
// Removes one attribute.
myTextBox.Attributes.Remove ( "maxlength" );
// Removes all attributes, clearing all properties.
myTextBox.Attributes.Clear ( );

// Creates comma-delimited list of defined attributes
string strTemp;
foreach ( string key in myTextBox.Attributes.Keys ) {
   strTemp += myTextBox.Attributes [ key ] + ", ";
}
  C# VB

See Also

Setting Web Forms Server Control Properties   Setting Web Server Control Properties Programmatically



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

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