ASP.NET Web Forms Web Forms Server Controls Programming Web Forms Server Controls
You can set Web server control properties programmatically to change the control’s appearance and behavior at run time. Properties for Web server controls are strongly typed, so the exact technique you use to set properties depends on what type of property you are setting.
NOTE: You set HTML server control properties slightly differently. For details, see Setting HTML Server Control Properties Programmatically.
If an Web server control property’s data type is a primitive — a String, Boolean, or numeric type — then you can set the property value by simply assigning it to the property. Similarly, if the property’s values are defined in an enumeration class, you can simply assign the enumeration to the property.
- Assign the value as a literal or variable, as in the following examples:
Label1.Text = "Hello";
DataGrid1.PageSize=5;
Label1.Text = "Hello"
DataGrid1.PageSize=5 |
|
C# |
VB |
- Assign the value using one of the enumeration values. The framework can resolve the enumeration based on the property’s type. The following illustrates setting a property using an enumeration:
// Uses TextBoxMode enumeration
TextBox1.Mode = TextBoxMode.SingleLine;
// Uses ImageAlign enumeration
Image1.ImageAlign=ImageAlign.Center;
' Uses TextBoxMode enumeration
TextBox1.Mode = TextBoxMode.SingleLine
' Uses ImageAlign enumeration
Image1.ImageAlign=ImageAlign.Center |
|
C# |
VB |
Some properties, notably measurements, are set in units. Units are implemented as objects ( the Unit class ), which allows you to specify a value and the measurement unit in various ways. The Unit class allows you to specify measurement units using the UnitType enumeration or using a string consisting of a numeric value plus a standard HTML unit abbreviation:
Abbreviation |
Unit |
px |
Pixel |
Pt |
Point |
Pc |
Pica |
in |
Inch |
cm |
Centimeters |
% |
Percentage |
Because units are manipulated in the Unit object, you must assign a reference from the Unit object to your object.
- Assign a reference to the Unit class to your control. The following examples show variations on how to do this.
[ Visual Basic ]
TextBox1.Width = New Unit ( 100 ) ' default is pixels
TextBox1.Width = New Unit ( 100, UnitType.Pixel )
TextBox1.Width = New Unit ( "100px" )
TextBox1.Width = New Unit ( "2cm" ) ' Centimeters
TextBox1.Width = New Unit ( 10, UnitType.Percentage )
TextBox1.Width = New Unit ( "10%" )
To set a property to a color ( such as the BackColor property ), you assign a reference to the Color object.
- Assign a reference to the Color object to your control. To specify the color, call the Color object’s FromARGB method, passing it a numeric value ( RGB ) or string name of the color. Alternatively, you can assign the color using a static method that references a predefined color name:
[ Visual Basic ]
Button1.BackColor=Color.FromARGB ( "Red" )
Button1.BackColor=Color.FromARGB ( 255, 255, 255 ) ' white ( in RGB )
Button1.BackColor=Color.Red
Button1.BackColor=Color.MediumSeaGreen ' HTML 4.0 color
The properties of some controls are not simple values or objects, but collections. For example, the individual values of a ListBox ASP.NET control are implemented as a collection of ListItem objects.
- Instantiate the item you want to use, and then add it to the control’s collection. The following example shows how to add a ListItem object to a listbox by adding it to the listbox’s Items collection. In the first example, the item is explicitly created before being added. In the second example, items are created and added at the same time.
[ Visual Basic ]
Dim li As ListItem
li=New ListItem
li.Text = "Item 1"
ListBox1.Items.Add ( li )
' Create and add the items at the same time
ListBox1.Items.Add ( New ListItem ( "Apples" ) )
ListBox1.Items.Add New ListItem ( "Oranges" )
ListBox1.Items.Add New ListItem ( "Lemons" )
Web Forms Server Controls and CSS Styles Setting HTML Server Control Properties Programmatically Setting Web Forms Server Control Properties