如何检查在C#中是否未选择ListBox的选定值?

时间:2021-07-10 07:20:35

This code will display the selected value from the listbox. E.g. if I sellect Item 1 I will get the following output: You have selected Item 1.

此代码将显示列表框中的选定值。例如。如果我选择第1项,我将得到以下输出:您已选择第1项。

Label1.Text = "You have selected " + DropDownList1.SelectedValue + "<br />";

But if I don't select anything and click on Submit button, I will get: You have selected

但如果我没有选择任何内容并点击“提交”按钮,我会得到:您已经选择了

What would I need to make it display "You have not selected anything. Please select at least 1 item."

我需要做什么才能显示“你没有选择任何东西。请选择至少1个项目。”

UPDATE: I am using ASP.NET WebForms.

更新:我正在使用ASP.NET WebForms。

2 个解决方案

#1


5  

Update:
The below answer is actually incorrect (left for history). Once you access the SelectedIndex property, when nothing is selected, the list will immediately make the first item selected, and return zero.

更新:以下答案实际上是不正确的(留给历史记录)。一旦访问SelectedIndex属性,当没有选择任何内容时,列表将立即选择第一个项目,并返回零。

So pretty much the only choice that remains is to have some kind of "dummy item" very first in the list, and check for SelectedIndex == 0.

所以剩下的唯一选择就是在列表中首先使用某种“虚拟项目”,并检查SelectedIndex == 0。

The above, however, is only true for DropDownList. Other controls derived from ListControl (i.e. ListBox or RadioButtonList) will correctly show SelectedIndex == -1.

但是,上述内容仅适用于DropDownList。从ListControl派生的其他控件(即ListBox或RadioButtonList)将正确显示SelectedIndex == -1。

Here goes the incorrect answer:
Check the SelectedIndex property. If nothing is selected, it will have the value of -1.

这里的答案是错误的:检查SelectedIndex属性。如果未选择任何内容,则其值为-1。

if ( DropDownList1.SelectedIndex < 0 )
{
    Label1.Text = "You have not selected anything";
}
else
{
    Label1.Text = "You have selected " + DropDownList1.SelectedValue;
}

#2


1  

Be carefull!:

Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot clear the selection from every item in the list at the same time.

使用SelectedIndex属性以编程方式指定或确定DropDownList控件中所选项的索引。始终在DropDownList控件中选择一个项。您无法同时清除列表中每个项目的选择。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex.aspx

#1


5  

Update:
The below answer is actually incorrect (left for history). Once you access the SelectedIndex property, when nothing is selected, the list will immediately make the first item selected, and return zero.

更新:以下答案实际上是不正确的(留给历史记录)。一旦访问SelectedIndex属性,当没有选择任何内容时,列表将立即选择第一个项目,并返回零。

So pretty much the only choice that remains is to have some kind of "dummy item" very first in the list, and check for SelectedIndex == 0.

所以剩下的唯一选择就是在列表中首先使用某种“虚拟项目”,并检查SelectedIndex == 0。

The above, however, is only true for DropDownList. Other controls derived from ListControl (i.e. ListBox or RadioButtonList) will correctly show SelectedIndex == -1.

但是,上述内容仅适用于DropDownList。从ListControl派生的其他控件(即ListBox或RadioButtonList)将正确显示SelectedIndex == -1。

Here goes the incorrect answer:
Check the SelectedIndex property. If nothing is selected, it will have the value of -1.

这里的答案是错误的:检查SelectedIndex属性。如果未选择任何内容,则其值为-1。

if ( DropDownList1.SelectedIndex < 0 )
{
    Label1.Text = "You have not selected anything";
}
else
{
    Label1.Text = "You have selected " + DropDownList1.SelectedValue;
}

#2


1  

Be carefull!:

Use the SelectedIndex property to programmatically specify or determine the index of the selected item from the DropDownList control. An item is always selected in the DropDownList control. You cannot clear the selection from every item in the list at the same time.

使用SelectedIndex属性以编程方式指定或确定DropDownList控件中所选项的索引。始终在DropDownList控件中选择一个项。您无法同时清除列表中每个项目的选择。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.selectedindex.aspx