Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable a ListItem?

I've got a DropDownList in ASP.NET that has a ListItem that requires being disabled... but I DON'T mean Enable="False". And I also don't want to disable the entire DropDownList, just one specific ListItem. What I'm talking about is written in HTML as disabled="disabled", like so:

<option disabled="disabled" value="-1">Disabled Option</option>

Anyone know how to do this in ASP.NET?

like image 945
Chuck Le Butt Avatar asked Oct 26 '10 09:10

Chuck Le Butt


People also ask

How do I disable a list item?

Use the . disabled class in Bootstrap to disable a list item in a list group in Bootstrap.

How to disable particular item in a drop down element?

Learning jQuery In this post, I will show you how you can disable specific items of the DropDown/ComboBox/Select element using jQuery. To disable any item, just need to add attribute "disabled" with value "disabled" to the list item.

How to disable list item in DropDownList?

The drop-down is used to create a list of items that need to select an element. We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element.

How to disable DropDownList in asp net?

If you are creating dropdown in code behind and want to disable it in code behind only, then you should use "Enabled" property and set it to false.


2 Answers

You could try setting the attribute from code behind, that way you can programmatically decide what value to be set.

So in your example you would do something like this:

var listItem = DropDownList.Items.FindByText("Your Item Text");
listItem.Attributes["disabled"]="disabled";    
like image 125
Simon Martin Avatar answered Oct 18 '22 17:10

Simon Martin


Have you tried adding disabled="disabled" on the ListItem element?

<asp:DropDownList runat="server" ID="id">
    <asp:ListItem Text="Test" Value="value" disabled="disabled" />
</asp:DropDownList>

Bear in mind that browser compatibility varies: http://www.lattimore.id.au/2005/06/18/disable-options-in-a-select-dropdown-element/

like image 33
Arnold Zokas Avatar answered Oct 18 '22 16:10

Arnold Zokas