3

I want to disable every fifth item in a dropdownlist.

dropdownlist1.items[i].Attributes.Add("disabled","disabled");

How do I write the logic to disable every fifth item in a Dropdownlist?

I am using two for loops: one for displaying items and one for disabling items in dropdownlist. How can I simplify my code?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Abbas
  • 4,948
  • 31
  • 95
  • 161
  • for(i=0;I<50;i++) { if(i%5==0) dropdownlist1.items[i].Attributes.Add("disabled","disabled"); else //do nothing and enable the the data dropdownlist1.items[i].Attributes.Add("enabled","enabled"); } – SharpUrBrain Mar 12 '11 at 15:21

5 Answers5

3

Perhaps you should consider just not showing them? As in:

if (i % 5 > 0) {
   dropdownlist1.items[i].Attributes.Add("disabled","disabled");
}
arnehehe
  • 1,386
  • 1
  • 17
  • 33
  • This doesn't "hide" them, but greys them out and keeps them from being selectable - just like the OP requested. – vapcguy Sep 25 '14 at 01:38
1
foreach(Control c in this.Controls)
 {
    if(c is dropdownlist)
    {
        dropdownlist dl = (dropdownlist)c;
        if (i % 5 > 0)
        {
           dl.items[i].Attributes.Add("disabled","disabled");
        }
    }
 }

Check this out! same as castirng.

findout all dropdownlist on the form and on every dropdown list it will disable.

let me know!!

bstpierre
  • 30,042
  • 15
  • 70
  • 103
Rafee
  • 3,975
  • 8
  • 58
  • 88
0

If what you need is like an optgroup functionality to group your options. Here is a sample of what an optgroup looks like in case you don't know http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_optgroup.

There are ways to add an optgroup to a dropdown list http://weblogs.asp.net/jeff/archive/2006/12/27/dropdownlist-with-optgroup.aspx

then you could use it like this

ListItem item = new ListItem("some group name", String.Empty);
item.Attributes["optgroup"] = "optgroup";
myDropDown.Items.Add(item);

or in your case

dropdownlist1.items[i].Attributes["optgroup"] = "optgroup";
Aivan Monceller
  • 4,636
  • 10
  • 42
  • 69
0

Using jQuery

var count = 0;
$('select option').each(function() {
   count++;
   if(count % 5 == 0)
     $(this).attr('disabled', 'disabled'); 
}):
Trevor
  • 11,269
  • 2
  • 33
  • 40
-1

I googled for "disable dropdownlist items" and the first result provided the exact answers you're looking for.

It's not possible, but there are alternatives.

Kon
  • 27,113
  • 11
  • 60
  • 86
  • On that very same link, user "Henxon" showed it WAS possible, using the same `items[i].Attributes.Add("disabled")` in the answers above on this page. In order to see the options get disabled, you would need to put it in a `Page_Load` or put the dropdown in a ContentTemplate of an AJAX UpdatePanel if you are doing it dynamically -- but if you are not using an UpdatePanel and you try to do it on the fly, just because the options don't disable, it does not mean it cannot be done - it's just not set up correctly. – vapcguy Sep 25 '14 at 01:35