如何在ASP.NET下拉列表中设置所选值?

时间:2021-10-24 07:34:32

I'm using an ASP.NET dropdown. I want to set the selected element using an ID (Id) which is stored in the DataValue field.

我正在使用ASP.NET下拉列表。我想使用存储在DataValue字段中的ID(Id)设置所选元素。

ddlTest.DataValueField = "Id";    
ddlTest.DataTextField = "Name";
ddlTest.DataSource = searchResultList;
ddlTest.DataBind();

But all I can see is the SelectedIndex property which isn't the correct thing. How do I set by value?

但我只能看到SelectedIndex属性,这是不正确的。我如何按价值设定?

3 个解决方案

#1


After it's bound you can set the SelectedValue property to the correct id.

绑定后,您可以将SelectedValue属性设置为正确的id。

#2


ddlTest.selectedValue = [whatever value you want to set];

ddlTest.selectedValue = [你要设置的任何值];

#3


If you need the text of the selected item then you can use

如果您需要所选项目的文本,则可以使用

dropdwonlistID.SelectedItem.Value // selecting value of the selected item
dropdwonlistID.SelectedItem.Text // selecting displayed text of the selected item

#1


After it's bound you can set the SelectedValue property to the correct id.

绑定后,您可以将SelectedValue属性设置为正确的id。

#2


ddlTest.selectedValue = [whatever value you want to set];

ddlTest.selectedValue = [你要设置的任何值];

#3


If you need the text of the selected item then you can use

如果您需要所选项目的文本,则可以使用

dropdwonlistID.SelectedItem.Value // selecting value of the selected item
dropdwonlistID.SelectedItem.Text // selecting displayed text of the selected item