How to add attributes to a ListBox items in asp.net

asp.net listbox attributes
listbox-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 = { 
                                 "Siberian Jay",
                                 "European Magpie",
                                 "Azure-winged Magpie",
                                 "Spotted Nutcracker",
                                 "Red-billed Blue Magpie",
                                 "Alpine Chough"
                             };

            ListBox1.DataSource = birds;
            ListBox1.DataBind();

            ListBox1.Rows = ListBox1.Items.Count;
        }
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (i % 2 == 0)
            {
                ListBox1.Items[i].Attributes.Add("style", "color:red;background-color:lightpink");
            }
            else
            {
                ListBox1.Items[i].Attributes.Add("style", "color:seagreen;background-color:beige");
            }
        }
        
    }
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net listbox attributes</title>  
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - listbox attributes
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <br /><br />  
        <asp:ListBox  
            ID="ListBox1"  
            runat="server"  
            AutoPostBack="true"  
            Font-Size="X-Large"
            SelectionMode="Multiple"
            Font-Names="Comic Sans MS"
            >  
        </asp:ListBox> 
    </div>        
    </form>        
</body>        
</html>
More asp.net examples