We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

[ASP.NET] Disable particular items in a dropdownlist

zandur
zandur asked
on
Medium Priority
789 Views
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?
Comment
Watch Question

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:

http://www.aspnetmania.com/Code/Code/128.html

The article is in russian... but C# is the same everywhere in the world so the code is all you need :)

Commented:
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");
dfu23,

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.

-Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Commented:
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 ... ?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.