Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Dropdownlist ListItem (Enabled=false) not showing in page

Tags:

c#

asp.net

I have a dropdownlist in aspx called ddlService.

I want to added the listitems from behind.

When I add, I will create them in order of Title and items underneath like..

Title1
Item1
Item2
Title2
Item1
Item2

Titles should not be able to click. Only Items should be able to click.

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);

The problem I encountered is The ListItems with (Enabled=false) are not appearing in aspx.

When I change it to (Enabled=true), it is appearing.

I must have missed out something. Can anyone point out?

Thanks.

like image 934
william Avatar asked Feb 07 '12 02:02

william


2 Answers

I believe this is what you're looking for. (Not tested)

tempServicesItem.Attributes.Add("disabled", "disabled");
like image 72
abney317 Avatar answered Nov 14 '22 22:11

abney317


MSDN documentation says

You cannot use this property to disable a ListItem control in a DropDownList control or ListBox control.

I think you need to set the "disabled" attribute which corresponds to the HTML markup for the option element

tempServicesItem.Attributes["disabled"] = "true";
like image 42
jhsowter Avatar answered Nov 14 '22 22:11

jhsowter