I have a dropdownlist control on a page with AutoPostBack set to True. On the initial load of the page I am setting a CSS class on certain ListItems in the DropDownList. The resulting HTML looks like this:
<select id="mySelect">
<option value="1">First</option>
<option value="2" selected="selected">Second</option>
<option value="3" class="favorite">Third</option>
<option value="4">Fourth</option>
<option value="5" class="favorite">Fifth</option>
</select>
After postback the ListItems lose their CSS classes. It now looks like this.
<select id="mySelect">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
<option value="4">Fourth</option>
<option value="5" selected="selected">Fifth</option>
</select>
Is there any way that the dropdownlist will remember the CSS classes on individual ListItems after a postback or will I need to somehow set the classes myself after postback?
Here is the code that adds the CSS to the dropdownlist. It is run on PageLoad but not run on PostBack.
foreach(MyItem _myItem in MyItemList)
{
ListItem _listItem = new ListItem();
_listItem.Value = _myItem.ID.ToString();
_listItem.Text = _myItem.Title;
if(_myItem.IsFavorite)
{
_list.Attributes["class"] = "favorite";
}
ddlMyDropDown.Items.Add(_listItem);
}
Corey