2011-10-13 84 views
0

我正在使用複選框列表綁定數據庫中的某些數據。 checkboxlist中的每個項目都設置了獨特的背景顏色。我需要將背景顏色設置爲複選框列表中的選定項目。對於綁定顏色,我已經使用下面的代碼如何獲取複選框列表中選定項目的背景顏色

for (int i = 0; i < cbFilter.Items.Count; i++)  
{ 
Color RandomColor = GetRandomColor(); 
string hexColorValue = ColorTranslator.ToHtml(RandomColor); 
cbFilter.Items[i].Attributes.Add("style", "background-color:" + hexColorValue); 
} 

請給出一個解決方案來獲取所選項目的背景顏色。

回答

0
  foreach (ListItem item in cbFilter.Items ) 
      { 
       if (item.Selected) 
       { 
        Response.Write(item.Attributes.CssStyle["background-color"].ToString()); 
       } 
      } 
+0

感謝您的回覆。我試過這個,但我得到的錯誤對象引用未設置爲對象的實例。 –

+0

@ user.net:請提供完整的代碼,以便我可以提供適當的解決方案 –

相關問題