我有一个DropDownList来填充服务器端,需要禁用一些将填充到此列表的选项。实际上,我知道我需要添加一个disabled='disabled'属性。
这是我第一次尝试。
Dim avListItems = activityVersions.Select(Function(av) New With {.Text = av.Text, .Value = av.Value, .Disabled=av.HasQuota}).ToList()
但这会禁用所有选项,因为在禁用HTML的属性中,如果有禁用的属性,则不需要值,否则无论如何都会禁用该选项。但是我需要一个可以为选项添加disabled属性的解决方案,它没有配额,其他的应该被启用。
你有什么建议吗?
不能禁用下拉列表中的单个项目。见 MSDN .
如果选择了一个无效的选项,或者从列表中完全删除这些项,那么可以应用一些验证来通知用户。
实际上可以禁用下拉列表中的项目。您需要做的是绑定列表,然后向其添加一个新属性。 下面的示例假设一个国家列表。
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"); } }