7

On my ASPX page I've added Dropdownlist.

Elements in this list are divided to groups by adding disabled list items:

ListItem separator = new ListItem("---My friends---", "");
separator.Attributes.Add("disabled", "true");
_ddUsersList.Items.Add(separator);

Those list items are grayed, I can't select it by mouse or by clicking cursor arrows(up/down). That is correct.

But problem is, that after clicking '-' key this list item is selected. I think that it is Dropdownlist bug, but I need to find some solution for this.

How to prevent selecting disabled ListItems by clicking first letter from its title? Or there is better way to create separators in Dropdownlist?

Edit: I've checked it after Nico G. comment. This problem happens in IE, not in Firefox. (I have no other browsers. Two is enought:) )

Marek Kwiendacz
  • 9,524
  • 14
  • 48
  • 72
  • 1
    I would have some javascript running after select which reset selection if the one starting with "-" was selected – Davide Piras Feb 13 '12 at 09:39
  • 1
    that's a bug in IE. check this link [Select, Option, Disabled And The JavaScript Solution](http://www.lattimore.id.au/2005/07/01/select-option-disabled-and-the-javascript-solution/)... in case you are experiencing the bug in other browsers, please update your question. just interested – Nika G. Feb 13 '12 at 10:36
  • Niko G. : you are right it happens in IE not in firefox. Write it as an answer, please. It is good solution for me. – Marek Kwiendacz Feb 13 '12 at 19:22
  • @MarekKwiendacz that's nice to see that the article solved your problem. As for the comment, it will be posted as answer. – Nika G. Feb 14 '12 at 06:18

2 Answers2

1

There are a number of different approaches in this question: Dropdownlist control with <optgroup>s for asp.net (webforms)? for adding Option Groups to a Dropdownlist control.

You can utilise the OptionGroups to create non-selectable separators which would look like the following in the source:

<select>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<optgroup label="----------"></optgroup>
<option>Option a</option>
<option>Option b</option>
<option>Option c</option>
</select>
Community
  • 1
  • 1
thudbutt
  • 1,481
  • 1
  • 19
  • 32
1

that's a bug in IE. check this link Select, Option, Disabled And The JavaScript Solution.

Nika G.
  • 2,374
  • 1
  • 17
  • 18