获取基于SelectedIndex的Dropdownlist值

时间:2012-09-15 16:44:39

标签: c#

如何从特定索引中选择值,即DropDownList的0,第1,第2,第3或第4个。

使用C#。

我有一个包含6个项目的DropDownList,现在我喜欢选择DropDownList的第4个项目表单。

请注意,此项目的值未知;因为它可能不时变化;但我知道它会在下拉列表中排在第4位。

我这是我的尝试,哪个无效

  

string Item4;

     

item4 = DropDownList1.SelectedIndex = 3;

6 个答案:

答案 0 :(得分:1)

string item4 = DropDownList1.Items [3] .Value;

答案 1 :(得分:1)

要通过以下索引设置Dropdown的值:

DropDownList1.SelectedIndex = 3;

要从下拉列表中获取值,请使用:

String Value = DropDownList.SelectedItem.Value;

用于文字:

String Text = DropDownList.SelectedItem.Text;

答案 2 :(得分:1)

 string item4 = DropDownList1.Items[3].Value;

 item4 = DropDownList1.SelectedIndex = 3; is not correct.

答案 3 :(得分:1)

var listItem = dropDownList.Items[3];
var value = listItem.Value;

答案 4 :(得分:0)

DropDownList1.SelectedIndex = 3;
string item = DropDownList.SelectedItem.Value;

答案 5 :(得分:0)

这里有很多答案

ListItem item = DropDownList1.Items[3];
string value = item.Value;
string text = item.Text;