asp.net.ph

Skip Navigation Links

Setting Web Server Control Properties Programmatically

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.

Setting Properties Based on Simple Values or Enumerations

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.

To set a property value based on simple values

Label1.Text = "Hello";
DataGrid1.PageSize=5;
  C# VB

To set a property value based on an enumeration

// Uses TextBoxMode enumeration
TextBox1.Mode = TextBoxMode.SingleLine;
// Uses ImageAlign enumeration
Image1.ImageAlign=ImageAlign.Center;
  C# VB

Setting Unit Properties

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.

To set unit-based properties

 [ 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%" ) 

Setting Color Properties

To set a property to a color ( such as the BackColor property ), you assign a reference to the Color object.

To set properties to a color

 [ 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

Setting Control Properties in Collections

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.

To set a complex control property

 [ 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" ) 
See Also

Web Forms Server Controls and CSS Styles   Setting HTML Server Control Properties Programmatically   Setting Web Forms Server Control Properties

Check out related books at Amazon

© 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