asp.net - How do I check if selected value of the ListBox is not selected in C#? -


This code will display the selected value from the list. Like if I select Item 1 , then I will get the following output: You have selected item 1 .

  Label1.Text = "You have selected" dropdown list 1. Selected value "
";

But if I do not choose anything and I click the submit button, then I will get: you have chosen

To display what should be "You have not selected anything. Please select at least 1 item."

Update: I am using ASP. Net Webform

Update:
Answer below is actually false (Omitted for history) Once you use the selected index property, nothing is selected, the first thing will be selected immediately in the list, and return zero.

The first thing in the list is "dummy item", and SelectedIndex == 0 .

Check for, however, only DropDownList . Correctly SelectedIndex == -1 will show other controls obtained from ListControl (i.e. ListBox or RadioButtonList )

Here is an incorrect answer :
selected index check property if nothing is selected, then its -1 Value will be there.

  if (dropdownlist 1. selected index <) {Label1.Text = "you have chosen nothing"; } Else {Label1.Text = "You have selected" + Dropdownlist 1. Selected value; }  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -