I can’t seem to figure this out. I have an asp DropDownList control. This gets populated by a class list of data I get from SQL. The DropDownList is in an ASP Repeater. I added an OnSelectedIndexChanged property to the asp control that fires a C# method. I have some logic in the initializing method for the DropDownList that sets a specific item’s text color to red (depending on a value in that items class). Whenever that item is selected the text is not red. The text is only red when it is an item within the list. How can I change the color of the selected item? I cannot seem to figure this out. I need the OnSelectedIndexChanged method on the C# side to fire an event to filter another selector. So I am already firing an event when the selector is changed. However I have tried the following:
protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender != null)
{
DropDownList DropDownList1 = (DropDownList)sender;
DropDownList1.SelectedItem.Attributes.Add("color", "Red;");
}
}
The above does not work. But I don’t think that is the issue. After inspecting the element (before the above implementation) the style color property is already set to red. Do these just not apply to the currently selected item?
I was able to set the text color of the entire list by setting the forecolor. But I can’t set a single element’s forecolor.
I don’t think you can generally do this, apart from in some of the latest versions of firefox and chrome. Traditionally we’ve never had much styling control on the HTML SELECT element, as they were originally just a standard desktop UI widget schlepped into the page.
See this answer for more details: How to style a <select> dropdown with CSS only without JavaScript?
That said, are you sure you should not be setting the
Style["color"]on theoption, rather than anattribute?