2017-01-30 67 views
0

我想設置一個下拉列表中的某些項目的背景圖片 但是,這不工作。現在ASP DropDown項目屬性

,這個工程

ddl.Items[0]?.Attributes.CssStyle.Add("background-color", "red"); 
ddl.Items[0]?.Attributes.CssStyle.Add("color", "blue"); 

的背景爲紅色,藍色文字的第一個項目,其餘爲正常

然而這一點,

ddl.Items[0]?.Attributes.CssStyle.Add("background-image", "url('ico.png')"); 

沒有。

我也嘗試過這兩種情況(下面)。同樣的結果

ddl.Items[0]?.Attributes.Add("style", "background-image:url('ico.png');"); 

當我設置在ASPX本身background-image屬性

<asp:DropDownList Style="background-image: url('ico.png')" id=../> 

它的存在的控制之下,而不是在列表中延伸到選擇一個項目..

我已經閱讀了互聯網,你甚至無法設置簡單的顏色,但它對我有用,所以如果他們稍後添加了該功能,可能是因爲某些原因未添加此特定屬性。

或者,如果您想到另一種將圖標添加到特定ddl項目的方式,請隨時鏈接/建議。

非常感謝!

回答

0
  DropDownList1.DataSource = objdt; 
      DropDownList1.DataTextField = "Country"; 
      DropDownList1.DataValueField = "Id"; 
      DropDownList1.DataBind(); 
      //Now add class to each and every item in dropdown 
      //Now, add a "SysCode" attribute to each item in the dropdown list 
      string imageURL = ""; 
      for (int i = 0; i < DropDownList1.Items.Count; i++) 
      { 
       switch (DropDownList1.Items[i].Text) 
       { 
        case "India": imageURL = "Images/flag-of-India.png"; 
         break; 
        case "Kuwait": imageURL = "Images/flag-of-Kuwait.png"; 
         break; 
        case "Egypt": imageURL = "Images/flag-of-Egypt.png"; 
         break; 
        case "Bangladesh": imageURL = "Images/flag-of-Bangladesh.png"; 
         break; 
        case "Afghanistan": imageURL = "Images/flag-of-Afghanistan.png"; 
         break; 
       } 
       ListItem item = DropDownList1.Items[i]; 
       item.Attributes["style"] = "background: url(" + imageURL + ");background-repeat:no-repeat;"; 
      } 
     } 
    } 
+0

http://www.aspdotnet-pools.com/2014/09/dropdownlist-item-with-custom-icon.html – Shum

+0

似乎並沒有得以順利,對不起。 – Poody