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.
- 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.";
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.
- 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 ] + ", ";
}
' Adds 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
Dim strTemp As String
For Each key As String In myTextBox.Attributes.Keys
strTemp &= myTextBox.Attributes ( key ) & ", "
Next |
|
C# |
VB |
Setting Web Forms Server Control Properties Setting Web Server Control Properties Programmatically
|