简体   繁体   中英

Error in color drop down list items

I have the following method which add an items to drop down list in run time. It is add the items good, but items appears without any style

Here the drop down list

<asp:DropDownList ID="ddlColorPicker" runat="server">                   
</asp:DropDownList>

And here the method that i call to add items on page-load

protected void fillDDLFilesTypesColor()
    {           
        ListItem l1 = new ListItem("بلا لون", "");
        l1.Attributes.Add("style", "color:red");
        l1.Attributes.Add("style", "background-color:#111111");

        ListItem l2 = new ListItem("أحمر", "red");
        l2.Attributes.Add("style", "color:red");
        l2.Attributes.Add("style", "background-color:#111111");            

        ListItem l3 = new ListItem("أزرق", "blue");
        l3.Attributes.Add("style", "color:blue");
        l3.Attributes.Add("style", "background-color:#00FF7F");

        ListItem l4 = new ListItem("أخضر", "green");
        l4.Attributes.Add("style", "color:green");
        l4.Attributes.Add("style", "background-color:#00FF7F");

        ListItem l5 = new ListItem("أصفر", "yellow");
        l5.Attributes.Add("style", "color:yellow");
        l5.Attributes.Add("style", "background-color:#111111");

        ddlColorPicker.Items.Add(l1);
        ddlColorPicker.Items.Add(l2);
        ddlColorPicker.Items.Add(l3);
        ddlColorPicker.Items.Add(l4);
        ddlColorPicker.Items.Add(l5);
    }

You are overwriting your style. Do it all in one line, like:

ListItem l2 = new ListItem("أحمر", "red");
l2.Attributes.Add("style", "background-color:#111111; color:red;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM