繁体   English   中英

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

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

大家好,我在检查列表中的项目时现在有一个包含某些项目的下拉列表,如果该项目存在于下拉列表中,我想为该特定项目应用颜色。

假设我的下拉列表如下

    123
    1234
    12345

现在,如果我找到123我想为该特定元素应用颜色有任何帮助,请

我只是尝试了一个示例,我不知道它是否对您有用,这仅仅是根据您的要求进行的示例尝试

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;");
    }
  }
}

根据您的第二个要求

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;");
  }
 }
 }
 }

这不是直接的解决方法,但我认为您可以使用以下方法来检索,修改和重新创建下拉列表的属性。

您是否尝试过访问iten的属性集合。 它包含项目的css类,但不能直接编辑。

dropdown.Items[0].Attributes.CssStyle

您将必须检索该集合,将其复制,定义一个新的css属性,清除旧属性并分配一个新属性。

暂无
暂无

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

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