2014-04-07 22 views
0

我具有其中我使用的是列表框控制來從數據庫中的數據一個asp.net Web應用程序。我在數據庫中有3組數據,我想在提取時對這些項目進行分組,並在列表框中顯示每個組的標題。我在網上做了一些研究,但是我發現的所有文章都使用WPF,並且我不能在我的案例中使用WPF,因爲我的應用程序是一個asp.net Web應用程序,現在我無法更改它。請幫助我找出是否有任何方法使用C#和asp.net將圖中所示的項目分組。組asp.net web應用列表框項目(不使用WPF)

在此先感謝

enter image description here

回答

1

您可以使用CSS類設置列表中每個項目的外觀/感受:

CSS:

<style> 
    .red 
    { 
     background-color:pink; 
     color:red;  
    } 
</style> 

ASPX:在必須使用類的每個項目標題= 「紅」 和禁用= 「禁用」(感謝@Arman

<asp:ListBox ID="ListBox1" runat="server"> 
     <asp:ListItem Text="A" Disabled="disabled" Class="red"></asp:ListItem> 
     <asp:ListItem Text="Adam"></asp:ListItem> 
     <asp:ListItem Text="Ami"></asp:ListItem> 
     <asp:ListItem Text="B" Disabled="disabled" Class="red"></asp:ListItem> 
     <asp:ListItem Text="Bob"></asp:ListItem> 
     <asp:ListItem Text="K" Disabled="disabled" Class="red"></asp:ListItem> 
     <asp:ListItem Text="Kim"></asp:ListItem> 
     <asp:ListItem Text="L" Disabled="disabled" Class="red"></asp:ListItem> 
     <asp:ListItem Text="Lena"></asp:ListItem> 
     <asp:ListItem Text="Leo"></asp:ListItem> 
     <asp:ListItem Text="Limi"></asp:ListItem> 
    </asp:ListBox> 

代碼隱藏:既然你被動態地從一個數據庫填充ListBox中的項目,您將需要設置標題項目的CSS屬性,像這樣:

ListItem x = new ListItem("A")); 
x.Attributes.Add("class","red"); 
ListBox1.Items.Add(x); 
+0

,更好地利用'禁用=「禁用」'各組頭 – Arman

+0

@Arman:對於somereason,這樣做改變了標題項目的前景色,爲「禁用」,並設置爲灰色項目重建基金olor,但是BG的顏色保持不變。我認爲指針事件:無;會解決這個問題,但沒有......我仍然在研究這個,但很好的評論! –

+0

感謝您的答覆arman。這很有幫助,但我仍然有一個問題。我的列表框中的數據是從數據庫中提取的,而不是將它靜態添加爲asp.net頁面上的列表項。你能告訴我如何從數據庫動態地獲取數據,如何添加css。 – user545359

0

聽起來好像這是你在找什麼。 How can I add option groups in ASP.NET drop down list?

這裏有客戶端和服務器端的解決方案。

+0

這是下拉列表而不是列表框,雖然有用,因爲 – Arman

+0

雖然我沒有確認,在CodeProject上的文章它鏈接到具有在使其與一個ListBox一個 http://www.codeproject.com/Articles/15505/ASP-NET工作的意見線程-DropDownList與 - 選項組支持 – InbetweenWeekends