[ASP.NET] Disable particular items in a dropdownlist

I have a dropdownlist that I populate in the code-behind.

In this dropdownlist I want to disable some listitems to prevent the user from selecting them . they are in the list for clarification only.

Is this possible to do from the code behind?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you want the item to be visible, but not selectable your best bet is to make the items an <optgroup> tag.

here is an article that shows you how to create a control to do this:


The article is in russian... but C# is the same everywhere in the world so the code is all you need :)
I would really suggest against using OPTGROUP because that is not what that tag is intended for ... unless grouping of OPTIONs is what you really need to do.

Instead I would recommend adding a disabled attribute to the individual OPTIONS that should be DISABLED:

myDropDownList.Items[0].Attributes.Add("disabled", "disabled");

Depending on what zandur means by "clarification" it may be exactly what OPTGROUP is intended for.  If not, while I agree that using the disabled attribute is a better option from a standards viewpoint, and if Section 508, or other accessibilty requirements are a concern it is even more important.  However, merely adding the attribute does not prevent it from being selected... custom client & server validation would need to be written to enforce the "disabled" nature of the control.  Additionally, you would likely want to add a class (to visually differentiate the item from selectable items) to the option as well.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Completely agreed with the OPTGROUP ... we really don't know exactly what zandur is going for so it's hard to say for sure without more input from him.

The sad thing about this is that you are right about the additional validation that you would need to do for Internet Explorer in order to ensure an OPTION that is supposedly disabled is not chosen. FireFox and most other browsers allow individual OPTIONs to be disabled and they act as expected ... grayed out and not selectable. I wonder if IE7 improves upon this ... ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.