简体   繁体   中英

How can i change the background color or apply an color to an Item in Drop down list

Hi all i am having a drop down list with some items now while checking the items from the list and if that item exists in the drop down i would like to apply color for that particular item.

Assume i have my drop down as follows

    123
    1234
    12345

Now if i found 123 i would like to apply color for that particular element any help please

I just tried a sample i don't know whether it works for your or not this is just a sample try as per your requirement

lst=new ListItem("123");
if (DropDownList1.Items.Contains(lst))
{
   for (int i = 0; i < DropDownList1.Items.Count; i++)
   {
     if (DropDownList1.Items[i].Equals(lst))
     {
        DropDownList1.Items[i].Attributes.Add("style", "background-color: red;");
    }
  }
}

As per your second requirement

ListItemCollection lstr = new ListItemCollection();
lstr.Add("123");
lstr .Add("1234");
foreach (ListItem lst in lstr)
{
if (DropDownList1.Items.Contains(lst))
{
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
  if (DropDownList1.Items[i].Equals(lst))
  {
    DropDownList1.Items[i].Attributes.Add("style", "background-color: red;");
  }
 }
 }
 }

this isn't a direct asnwer but I would think you could use the following methods to retrieve, modify, and recreate the attributes for the drop down list.

Have you tried accessing the iten's attribute collection. It contains the item's css class but it's not directly editable.

dropdown.Items[0].Attributes.CssStyle

You'll have to retrieve the collection, copy it, define a new css attribute, clear the old one and assign a new one.

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