How to: Set ASP.NET Server Control Style Properties Programmatically
You can set style properties of an ASP.NET server control programmatically, which allows you to change the appearance of a control conditionally.
To set style properties programmatically
Use the following hierarchical convention for specifying the style object and property you want to set:
The following code example shows how to set theproperty for the object of a control:
You can also create a separateobject and copy or merge the values of its properties to one of the styles on a control. This is a powerful way to apply the same styles to several different list objects in your project. In effect, you can create a virtual style sheet as a Style object and then apply it to a series of controls.
To create a style object and apply it to controls
Create an instance of the Style object and set its properties, as shown in the following example:
Assign the Style object to a control using one of the following methods:
Themethod applies all settings from a Style object, including null ones.
Themethod copies only the properties already set on the Style object, skipping properties that have not been set. Also, the MergeWith method will not overwrite any existing style elements.
The following code example shows how you might create a Style object, set one of its properties, and then apply it to two different control style objects, using each of the methods described: