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.
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.”
UPDATE: I am using ASP.NET WebForms.
Update:
The below answer is actually incorrect (left for history). Once you access the
SelectedIndexproperty, when nothing is selected, the list will immediately make the first item selected, and return zero.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.The above, however, is only true for
DropDownList. Other controls derived fromListControl(i.e.ListBoxorRadioButtonList) will correctly showSelectedIndex == -1.Here goes the incorrect answer:
Check the
SelectedIndexproperty. If nothing is selected, it will have the value of -1.