asp.net dropdownlist item background color
dropdownlist-item-background-color.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = {
"Southern Screamer",
"Horned Screamer",
"Swan Goose",
"Greylag Goose",
"Snow Goose",
"Canada Goose",
"Barnacle Goose",
"Canada Goose",
"Nene"
};
DropDownList1.DataSource = birds;
DropDownList1.DataBind();
}
string[] colors = {"snow","aliceblue","linen","cornsilk" };
int index = 0;
foreach (ListItem li in DropDownList1.Items)
{
string bg = "background-color:";
bg += colors[index];
li.Attributes.Add("style", bg);
index++;
if (index == colors.Count())
{
index = 0;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist item background color</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist item background color
</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 get DropDownList selected value by selectedindexchanged event
- How to set a default value for DropDownList
- How to add an item at the top of DropDownList
- How to add an item at DropDownList index 0
- How to add an item to DropDownList after databind
- How to add a blank item at the top of DropDownList
- How to make DropDownList first item not selectable
- How to display tooltip for DropDownList each items
- How to highlight an item in a DropDownList
- How to add vertical space between items in a RadioButtonList