This documentation is archived and is not being maintained.

Setting Web Server Control Properties Programmatically

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. For example, setting a unit property such as width is different from setting a color property. The sections below provide details about setting different types of properties. For information about the properties for a specific control, search for the name of the control class (for example, "Button class (System.Web.UI.WebControls)" in the Help index.

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 a 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

  • Assign the value as a literal or variable, as in the following examples:
    ' Visual Basic
    Label1.Text = "Hello"
    DataGrid1.PageSize = 5
    // C#
    Label1.Text = "Hello";
    DataGrid1.PageSize = 5;

To set a property value based on an enumeration

  • 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:
    ' Visual Basic
    ' Uses TextBoxMode enumeration
    Me.TextBox1.TextMode = TextBoxMode.SingleLine
    ' Uses ImageAlign enumeration
    Me.Image1.ImageAlign = ImageAlign.Middle
    // C#
    // Uses TextBoxMode enumeration 
    this.TextBox1.TextMode = TextBoxMode.SingleLine;
    // Uses ImageAlign enumeration
    this.Image1.ImageAlign = ImageAlign.Middle;

Setting Unit Properties

Width, height, and similar properties are set in units. Units are implemented as objects (the Unit structure), which allows you to specify a value and the measurement unit in various ways.

To set unit-based properties

  • Assign a reference to the Unit structure to your control. The following examples show variations on how to do this.
    ' Visual Basic
    ' Default is pixels.
    TextBox1.Width = New Unit(100)
    TextBox1.Width = New Unit(100, UnitType.Pixel)
    TextBox1.Width = New Unit("100px")
    ' Centimeters.
    TextBox1.Width = New Unit("2cm")
    TextBox1.Width = New Unit(10, UnitType.Percentage)
    TextBox1.Width = New Unit("10%")
    // C#
    // Default is pixels.
    TextBox1.Width = new Unit(100);
    TextBox1.Width = new Unit(100, UnitType.Pixel);
    TextBox1.Width = new Unit("100px");
    // Centimeters
    TextBox1.Width = new Unit("2cm");
    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

  • Assign a reference to the Color structure 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.FromName("Red")
    ' White in RGB.
    Button1.BackColor = Color.FromArgb(255, 255, 255)
    Button1.BackColor = Color.Red
    ' HTML 4.0 Color
    Button1.BackColor = Color.MediumSeaGreen
    // C#
    Button1.BackColor = Color.FromName("Red");
    // White in RGB.
    Button1.BackColor = Color.FromArgb(255, 255, 255);
    Button1.BackColor = Color.Red;
    // HTML 4.0 color.
    Button1.BackColor = Color.MediumSeaGreen;

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 Web server control are implemented as a collection of ListItem objects.

To set a complex control property

  • 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 list box by adding it to the list box'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 = New ListItem("Item 1")
    ' 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"))
    // C#
    // Create the items and then add them to the list.
    ListItem li = new ListItem("Item 1");
    // 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

Customizing the Appearance of ASP.NET Server Controls Using Styles | Setting HTML Server Control Properties Programmatically | Setting ASP.NET Server Control Properties