Populate drop down list with current user value as the selected drop down list option asp

559 views Asked by At

I would like to know the best approach on populating a drop down list with all database values, AND have the current value for a user be the selected index. I have the drop down list being populated with the database values, but I'm not sure on how you select. Thank you

1

There are 1 answers

0
Abhinav Galodha On
  1. Set the DataSource Property of the Dropdown control to the dataSource. The DataSource can be a List or any other kind of DataSource.

  2. Set the DataTextField property, which represent the text, which is displayed for each item in the dropdown list.

  3. Set the DataValueField property, which represent the Unique Value for each item in the dropdown list.

  4. Call the DataBind method of the DropdownList.

  5. To set the selected item, set the SelectedIndex property of the dropdown to the index which needs to be selected.

Following code snippet shows how it can be done..

private void LoadDropdown()
{
     dropdownList.DataSource = YourDataSource;
     dropdownList.DataTextField = "DisplayPropertyName";
     dropdownList.DataValueField = "ValuePropertyName";

     // Bind the data to the control.
     dropdownList.DataBind();

     // Set the default selected item, if desired.
     dropdownList.SelectedIndex = indexOfItemWhichShouldBeSelected;
}