2012-02-07 32 views
3

我有一個dropdownlist在aspx中調用ddlServiceASP.Net Dropdownlist ListItem(Enabled = false)不在頁面中顯示

我想從後面添加listitems。

當我加入,我會在標題和項目的順序下,如創建他們..

Title1 
Item1 
Item2 
Title2 
Item1 
Item2 

標題不應該是能夠點擊。只有項目應該能夠點擊。

ListItem tempServicesItem = new ListItem(); 
tempServicesItem.Text = tempTitle; 
tempServicesItem.Value = tempTitle; 
tempServicesItem.Enabled = false;  
ddlServices.Items.Add(tempServicesItem); 
tempServicesItem = new ListItem(); 
tempServicesItem.Text = tempItem; 
tempServicesItem.Value = tempItem;               
ddlServices.Items.Add(tempServicesItem); 

我遇到的問題是ListItems與(Enabled=false)都沒有出現在ASPX。

當我將其更改爲(Enabled=true)時,它正在出現。

我一定錯過了一些東西。任何人都可以指出嗎?

謝謝。

回答

6

我相信這是你在找什麼「已禁用」屬性。 (未測試)

tempServicesItem.Attributes.Add("disabled", "disabled"); 
+0

是的,我剛剛發現那也。不管怎麼說,還是要謝謝你。有關更多參考資料,http://forums.asp.net/t/1132654.aspx(閱讀至最後) – william 2012-02-07 02:58:27

+1

我發現了一個呃逆。如果我選擇其中一個值並刷新頁面。之後,屬性消失了。 – william 2012-02-07 03:03:05

+0

我對自己有一個解決方法。這將會給'Titles'的'value'一些獨特的名字,比如'Title_Unclickable'。然後在'(IsPostBack)'的每個'page_load'循環下拉列表並將這個'disable'屬性應用於所有那些具有上面提到的值的項目。 – william 2012-02-07 03:17:16

0

MSDN documentation

不能使用這個屬性來禁止在 DropDownList控件或ListBox控件ListItem控件。

我認爲你需要設置對應於HTML標記option元素

tempServicesItem.Attributes["disabled"] = "true";