如何根据特定条件隐藏/显示下拉列表项?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何根据特定条件隐藏/显示下拉列表项?相关的知识,希望对你有一定的参考价值。

这是我的下拉前端代码:

<asp:DropDownList ID="ddlSearch" runat="server" CssClass="form-control">
    <asp:ListItem Value="1" Text="Ref No" />
    <asp:ListItem Value="2" Text="Name" />
    <asp:ListItem Value="3" Text="contact" />
</asp:DropDownList>
答案

您可以在.FindByValue上使用.FindByTextddlSearch.Items方法获取该项目。然后将项目的.Enabled属性设置为false将在UI上隐藏它。

因此,例如,如果您需要隐藏值为“1”的项目,则可以执行以下操作:

ddlSearch.Items.FindByValue("1").Enabled = false;

请注意,在生产代码中,您希望在FindByValue之后进行空检查,但为了简单起见,我省略了它。例如,以下是检查空值的方法:

var listitem = ddlSearch.Items.FindByValue("1");
if (listitem != null) 
{
    listitem.Enabled = false;
}
else 
{
    // Throw an exception or display an error that the list item wasn't found.
}

以上是关于如何根据特定条件隐藏/显示下拉列表项?的主要内容,如果未能解决你的问题,请参考以下文章