How to set item in DropDownList as default value?

How to set an item in a DropDownList as the default value in ASP.NET?

SomeDropDownList.DataSource =GetSomeStrings();
+3
source share
3 answers

There are two ways to do this:

  • Set the SelectedValuevalue that you want to use by default DropDownList.SelectedValue = "value". This is very simple, but will result in an error if the drop-down menu already hasSelectedValue

  • Set the actual item DropDOwnList.Items.FindByValue("value").Selected = true;that should not result in an error if the selected item is already selected.

+2
source

Set SelectedValueproperty

+1
source

DropDownList , SelectedValue:

someDropDownList.DataSource = GetSomeStrings();
someDropDownList.DataBind();
someDropDownList.SelectedValue = "default value";

, SelectedIndex:

someDropDownList.SelectedIndex = 0;
+1

Source: https://habr.com/ru/post/1726952/


All Articles