2012-09-15 128 views
1

如何从特定索引中选择值,即DropDownList的0,1,2,3或4。基于SelectedIndex获取Dopdownlist值

使用C#。

我有一个DropDownList有6个项目,现在我喜欢选择DropDownList的第四个项目窗体。

请注意,该项目的价值是未知的;因为它可能会改变时间;但我知道它将在下拉的第四位。

我这是我的尝试,这不是工作

串项目4; item1 = DropDownList1.SelectedIndex = 3;

item4 = DropDownList1.SelectedIndex = 3;

+0

它是如何不工作?你有错误吗?意外的结果? – driis

+0

另外,这是ASP .NET还是WinForms?据我所知,这两个都有一个DropDownList类。不用猜测就好了。 – driis

回答

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

item4 = DropDownList1.SelectedIndex = 3; is not correct. 
0
DropDownList1.SelectedIndex = 3; 
string item = DropDownList.SelectedItem.Value; 
1

串ITEM4 = DropDownList1.Items [3]。价值;

1

要通过以下指标使用设置下拉值:

DropDownList1.SelectedIndex = 3; 

要想从下拉列表中使用的值:

String Value = DropDownList.SelectedItem.Value; 

而对于文字使用:

String Text = DropDownList.SelectedItem.Text; 
1
var listItem = dropDownList.Items[3]; 
var value = listItem.Value; 
+1

这将选择第5项 – codingbiz

+0

@codingbiz,感谢您指出错误。我修好了。 – RePierre

0

这么多答案在这里

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