[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 755
  • Last Modified:

[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?
0
zandur
Asked:
zandur
  • 2
  • 2
2 Solutions
 
PraxisWebCommented:
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 :)
0
 
dfu23Commented:
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");
0
 
PraxisWebCommented:
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
0
 
dfu23Commented:
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 ... ?
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now