[ASP.NET] Disable particular items in a dropdownlist

Posted on 2006-03-24
Last Modified: 2008-01-16
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?
Question by:zandur
    LVL 13

    Expert Comment

    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 :)
    LVL 14

    Expert Comment

    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");
    LVL 13

    Accepted Solution


    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.

    LVL 14

    Assisted Solution

    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 ... ?

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    The viewer will learn how to dynamically set the form action using jQuery.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now