2014-09-19 54 views
1

列表框如何改變ListBox中選定項的背景色Asp.Net

<asp:ListBox ID="YearListBox" runat="server" Rows="11" SelectionMode="Multiple" AutoPostBack="True"></asp:ListBox> 

我怎樣才能設置所選項目的背景爲藍色。 我已經嘗試了以下解決方案,但無法在Chrome中正常工作。

int[] YearIndexes = YearListBox.GetSelectedIndices(); 
      for (int i = 0; i < YearIndexes.Length; i++) 
      { 
       //do something... 
       YearListBox.Items[YearIndexes[i]].Attributes.Add("style", "background-color:blue"); 
      } 

我們可以看到所選擇的項目的背景顏色在該圖像上[1]是灰色的,但項的背景屬性是這樣的圖像[2]上藍色。

看來,瀏覽器的默認樣式覆蓋了我的背景屬性。

+0

我想回發後,所選項目的背景顏色設置爲藍色。 – 2014-09-19 12:08:23

回答

0

首先,我會使用類而不是定義代碼中的顏色(它在未來更容易更改)。

編輯: 您要求改變UI的行爲方式,您可能會混淆用戶。據我所知,無法覆蓋應用於選定項目的默認灰色和藍色樣式。如果你必須這樣做,那麼你需要看看某種自定義控件。

這會是你想要的,所以你可以申請完全自定義它是什麼區域:reinventing a drop-down with css and jquery/

Custom SelectBox

+0

這不適用於多個選擇。