简体   繁体   English

使用当前用户值填充下拉列表作为选定的下拉列表选项asp

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

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. Set the DataSource Property of the Dropdown control to the dataSource. 将下拉控件的DataSource属性设置为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. 设置代表文本的DataTextField属性,该属性针对下拉列表中的每个项目显示。

  3. Set the DataValueField property, which represent the Unique Value for each item in the dropdown list. 设置DataValueField属性,该属性表示下拉列表中每个项目的唯一值。

  4. Call the DataBind method of the DropdownList. 调用DropdownList的DataBind方法。

  5. To set the selected item, set the SelectedIndex property of the dropdown to the index which needs to be selected. 要设置所选项目,请将下拉菜单的SelectedIndex属性设置为需要选择的索引。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM