簡體   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