Applying Styles to DropDownList Items

Changing Parameter and Style Properties of Server Controls with Code in Asp.Net

Most properties of Asp.Net server controls can be changed at runtime. We can apply new properties and styles to the elements using the Attributes.Add method.

Usage is as follows:

ControlId.Attributes.Add("parameter", "value");

In the example below, a different background color is applied to each element (Item) of the DropDownList control.

for (int i = 1; i <=8; i++ )
    DropDownList1.Items[i].Attributes.Add("style", "background-color:#C5D9F1;");
for (int i = 9; i <= 14; i++)
    DropDownList1.Items[i].Attributes.Add("style", "background-color:#FCD5B4");
for (int i = 15; i <= 32; i++)
    DropDownList1.Items[i].Attributes.Add("style", "background-color:#C4D79B");

Different background color has been applied to the elements of the list. The result will be as follows.


Color each element of dropdownlist, apply different styles to list elements, applying styles with code, apply css style to server controls


There are no examples related to this subject.


Read 349 times.

Online Users: 106