How to disable DropDownList option in asp.net server side?
I have a dropdown to populate the backend, some of the options to be populated in this list need to be disabled. In practice, I know that I need to add the disabled = 'disabled' attribute.
This is the first way I tried.
Dim avListItems = activityVersions.Select(Function(av) New With {.Text = av.Text, .Value = av.Value, .Disabled=av.HasQuota}).ToList()
But this disables all options because no value is needed in the HTML disabled attribute, if there is disabled attribute then it disables the option anyway. But I need a solution that can add a disabled attribute for parameters that have no quota and others must be enabled.
Do you have any suggestions?
Unable to disable individual items in a dropdown list. See MSDN .
Perhaps rather apply some validation to notify the user if an invalid option is selected, or to remove those items entirely from the list.
It is actually possible to disable the item in the dropdown. you need to bind the list and then add a new attribute to it. The example below provides a list of countries.
List<Country> countryList = CountryFunctions.CreateCountryCollection();
ddlCountryList.DataSource = countryList;
ddlCountryList.DataTextField = "Name";
ddlCountryList.DataValueField = "Code";
ddlCountryList.DataBind();
//Disable any preselected items in dropdown list
foreach (var country in countryList)
{
var disabledCountry = IsCountryDisabled(country.Code);
if (country.Id != 0)
{
ddlCountryList.Items.FindByValue(country.Code).Attributes.Add("disabled", "true");
}
}