asp.net dropdownlist highlight item
The following asp.net c# example code demonstrate us how can we highlight DropDownList item or
items based on a or many criteria. We can highlight multiple items from DropDownList at run time by conditions.
To highlight items from DropDownList server control, first we loop through the items collection. Then we check each items which text contains 'Goose' characters. If we found an item which 'Text' property value contains 'Goose' then we add a CSS style to this ListItem object. we simply add CSS 'background-color' and 'color' property with value to the specified ListItem object.
In this way we check all items from DropDownList and apply the CSS style to those ListItem object which fulfill the criteria. Finally, DropDownList render in web browser when some of its items are highlighted by different background and text color.
To highlight items from DropDownList server control, first we loop through the items collection. Then we check each items which text contains 'Goose' characters. If we found an item which 'Text' property value contains 'Goose' then we add a CSS style to this ListItem object. we simply add CSS 'background-color' and 'color' property with value to the specified ListItem object.
In this way we check all items from DropDownList and apply the CSS style to those ListItem object which fulfill the criteria. Finally, DropDownList render in web browser when some of its items are highlighted by different background and text color.
dropdownlist-highlight-item.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Black Swan",
"Mute Swan",
"Cackling Goose",
"Whooper Swan",
"Tundra Swan",
"Hawaiian Goose",
"Blue Duck",
"Common Shelduck"
};
DropDownList1.DataSource = birds;
DropDownList1.DataBind();
}
foreach (ListItem li in DropDownList1.Items)
{
if (li.Text.Contains("Goose"))
{
li.Attributes.Add("style", "background-color:crimson;color:white");
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist highlight item</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist highlight item
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
Width="350"
Font-Size="X-Large"
>
</asp:DropDownList>
</div>
</form>
</body>
</html>

- How to change Label width using CSS
- How to trim text with ellipsis in a Label
- How to add margin to a Label
- How to add an item to DropDownList after databind
- How to add a blank item at the top of DropDownList
- How to add attributes to DropDownList items
- How to add CSS class to DropDownList each items
- How to make DropDownList first item not selectable
- How to display tooltip for DropDownList each items
- How to change DropDownList item background color