简体   繁体   English

如何更改背景颜色或将颜色应用于下拉列表中的项目

[英]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 现在,如果我找到123我想为该特定元素应用颜色有任何帮助,请

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. 您是否尝试过访问iten的属性集合。 It contains the item's css class but it's not directly editable. 它包含项目的css类,但不能直接编辑。

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. 您将必须检索该集合,将其复制,定义一个新的css属性,清除旧属性并分配一个新属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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