Reputation: 5044
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?
Upvotes: 3
Views: 15011
Reputation: 4078
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!!
Upvotes: 1
Reputation: 11425
Using jQuery
var count = 0;
$('select option').each(function() {
count++;
if(count % 5 == 0)
$(this).attr('disabled', 'disabled');
}):
Upvotes: 0
Reputation: 4670
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";
Upvotes: 0
Reputation: 1398
Perhaps you should consider just not showing them? As in:
if (i % 5 > 0) {
dropdownlist1.items[i].Attributes.Add("disabled","disabled");
}
Upvotes: 3
Reputation: 27431
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.
Upvotes: -1