Charley W. Charley W. - 2 months ago 12x
ASP.NET (C#) Question

ASP:DropDownList Codebehind Add Color

Dropdown list needs to display items with varying text colors. Color is decided by server-side. Uses

tags versus CSS styling sheet. See example:

protected void Page_Load(object sender, EventArgs e)
ListItem li = CreateListItemWithColor("Hello", "myValue", "blue");

public ListItem CreateListItemWithColor(string text, string value, string color)
//Create the list item based on input text/value
ListItem li = new ListItem();
li.Text = text;
li.Value = value;
li.Attributes.Add("style", "color="+color);
return li;

From what I have read in other SO posts about formatting list item text, my general procedure seems close. But my ListItem is always black. What am I missing?

Abbreviated HTML:

#employeeDropDownBox {
height: 65px;
width: 425px;
font-size: 27px;
margin-left: 5px;


<asp:DropDownList ID="employeeDropDownBox" runat="server" ></asp:DropDownList>


Instead of adding style, add css class & use this class in your css file to handle the color

li.Attributes.Add("class", "blue"); // you can use the `color` parameter as well

In your css:

.blue {
    color: blue;


So, you need to use : instead of =

    li.Attributes.Add("style", "color:" + color);