2016-12-16 41 views

回答

1
  1. 将下拉控件的DataSource属性设置为dataSource。 DataSource可以是List或任何其他类型的DataSource。

  2. 设置DataTextField属性,该属性表示文本,该属性显示在下拉列表中的每个项目。

  3. 设置DataValueField属性,它表示下拉列表中每个项目的唯一值。

  4. 调用DropdownList的DataBind方法。

  5. 要设置所选项目,请将下拉菜单的SelectedIndex属性设置为需要选择的索引。

下面的代码片段展示它是如何做..

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