![](/img/trans.png)
[英]Disable Items in dropdownlist by entries in database in Asp.net C#
[英]How to Disable the button while selecting the items in combobox in C# asp.net
如果组合框中没有选择任何内容,如何禁用按钮? 这是我尝试过的代码
if (satelliteComboBox.Items.Count > 0)
{
displayProductionDataButton.Enabled = false;
}
我已经尝试过了,但是没有用,我非常感谢任何建议
在组合框集合的开头添加一个额外的项,即默认情况下“选择”或在加载组合框时选择的项应为“选择”,因此组合框的选定索引将为0,因此只需检查是否
if (comboBox1.SelectedIndex == 0)
{
//No item Selected
displayProductionDataButton.Enabled = false;
}
else
{
//item selected
}
首先,您应该为DropDownList设置AutoPostBack属性为true。 之后,在SelectedIndexChange事件中,您可以使用所需的代码,如下所示:
if (DropDownList1.SelectedIndex < 0)
{
Button1.Enabled = false;
}
else
{
Button1.Enabled = true;
}
在此代码中,当用户未选择任何项目时,按钮将被禁用。 或者,您可以将DropDownList的第一项设置为“ Choose ...”之类的值,然后将代码更改为此,这对您的用户来说更好:
if (DropDownList1.SelectedIndex <= 0)
{
Button1.Enabled = false;
}
else
{
Button1.Enabled = true;
}
试着
if (satelliteComboBox.Items.Count is nothing)
{
displayProductionDataButton.Enabled = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.