How to set ListBox alternate item different text and background color in asp.net

asp.net listbox alternate color
The following asp.net c# example code demonstrate us how can we apply alternate text color for ListBox items.

First, we loop through the ListBox items by for loop. Next, we separate the ListBox items based on for loop index odd and even number. On each odd number specified ListBox items, we applied a text color and each even number specified ListBox items we applied another text color. Listbox items alternating color provide a great user experience.

We applied the ListBox items text color using ListItem Attribute property. We added a CSS class to the ListItem Attribute collections. To do this we crated two different CSS classes for ListBox items. The both CSS classes contain color property and different value. Finally, the ListBox render its items to the browser with alternating text color.
listbox-alternate-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 = { 
                                 "Gila Woodpecker",
                                 "Yellow-bellied Sapsucker",
                                 "Red-breasted Sapsucker",
                                 "Lizard Buzzard",
                                 "Spotted Harrier",
                                 "Dark Chanting Goshawk"
                             };

            ListBox1.DataSource = birds;
            ListBox1.DataBind();
        }
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (i % 2 == 0)
            {
                ListBox1.Items[i].Attributes.Add("class", "even");
            }
            else
            {
                ListBox1.Items[i].Attributes.Add("class", "odd");
            }
        }
    }
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net listbox alternate color</title>  
    <style type="text/css">
        .even {
            color:darkred;
            background-color:gold;
        }
        .odd {
            color:hotpink;
            background-color:honeydew;
        }
    </style>
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - listbox alternate color
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />   
        <br />     
        <asp:ListBox  
            ID="ListBox1"  
            runat="server"  
            AutoPostBack="true"  
            Font-Size="X-Large"
            SelectionMode="Single"
            Height="250"
            Width="350"
            Font-Names="Comic Sans MS"  
            >  
        </asp:ListBox>  
    </div>        
    </form>        
</body>        
</html>
More asp.net examples