How do I disable a ListItem?

老子叫甜甜 提交于 2019-12-10 13:13:06

问题


I've got a DropDownList in ASP.NET that has a ListItem that requires being disabled... but I DON'T mean Enable="False". And I also don't want to disable the entire DropDownList, just one specific ListItem. What I'm talking about is written in HTML as disabled="disabled", like so:

<option disabled="disabled" value="-1">Disabled Option</option>

Anyone know how to do this in ASP.NET?


回答1:


Have you tried adding disabled="disabled" on the ListItem element?

<asp:DropDownList runat="server" ID="id">
    <asp:ListItem Text="Test" Value="value" disabled="disabled" />
</asp:DropDownList>

Bear in mind that browser compatibility varies: http://www.lattimore.id.au/2005/06/18/disable-options-in-a-select-dropdown-element/




回答2:


You could try setting the attribute from code behind, that way you can programmatically decide what value to be set.

So in your example you would do something like this:

var listItem = DropDownList.Items.FindByText("Your Item Text");
listItem.Attributes["disabled"]="disabled";    


来源:https://stackoverflow.com/questions/4022292/how-do-i-disable-a-listitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!