disable dropdownlist items

14,112

Solution 1

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

if (i % 5 > 0) {
   dropdownlist1.items[i].Attributes.Add("disabled","disabled");
}

Solution 2

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!!

Share:
14,112
Abbas
Author by

Abbas

Updated on July 15, 2022

Comments

  • Abbas
    Abbas almost 2 years

    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?

  • vapcguy
    vapcguy over 9 years
    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
    vapcguy over 9 years
    This doesn't "hide" them, but greys them out and keeps them from being selectable - just like the OP requested.