Vinz' Blog

"Code, Beer and Music" ~ my way of being a programmer!
posts - 123, comments - 343, trackbacks - 0

My Links

News

Archives

Image Galleries

Set Background Color for each ListItems of the DropDownList

The following snippet below describes on how we are going to set the ListItem Background Color based on the Color name displayed in the DropDownList


ASPX MARKUP OF DROPDOWNLIST


<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnLoad="DropDownList1_Load">
        <asp:ListItem Value="-1">Select</asp:ListItem>
        <asp:ListItem Value="0">Red</asp:ListItem>
        <asp:ListItem Value="1">Blue</asp:ListItem>
        <asp:ListItem Value="2">Green</asp:ListItem>
        <asp:ListItem Value="3">Violet</asp:ListItem>
</asp:DropDownList>

RELEVANT CODES
protected void DropDownList1_Load(object sender, EventArgs e)
{
        for (int i = 0; i < DropDownList1.Items.Count; i++)
        {
            DropDownList1.Items[i].Attributes.Add("style", "background-color:" + DropDownList1.Items[i].Text);
           
        }
}

That's it!

Print | posted on Friday, September 12, 2008 2:10 AM |

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: