asp.net dropdownlist attributes
dropdownlist-attributes.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
string[] birds = { "Emu", "Brown Kiwi", "Malleefowl", "Maleo", "Helmeted Guineafowl" };
DropDownList1.DataSource = birds;
DropDownList1.DataBind();
}
for (int i = 0; i < DropDownList1.Items.Count;i++)
{
if (i % 2 == 0)
{
DropDownList1.Items[i].Attributes.Add("class", "even");
}
else
{
DropDownList1.Items[i].Attributes.Add("class", "odd");
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net dropdownlist attributes</title>
<style type="text/css">
.even {
color:red;
background-color:snow;
}
.odd {
color:blue;
background-color:aliceblue;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net example - dropdownlist attributes
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="false"
Width="350"
Font-Size="X-Large"
>
</asp:DropDownList>
</div>
</form>
</body>
</html>
- How to trim text with ellipsis in 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 change DropDownList alternate item color
- How to change DropDownList selected item color
- How to change ListBox item background color
- How to apply ListBox auto height
- How to set RadioButtonList items vertical alignment
- How to add vertical space between items in a RadioButtonList