user3809554
user3809554

Reputation: 143

How to set the datatextfield value instead of the value on a drop down list?

I have a list of countries, which returns an ID and a Name. I also have a string. The drop down sets the value to the ID, but at this point I don't have the ID's. I want to be able to set the selectedvalue as the Text Field. Is there a way to do this? or is there a way to compare my string against the list to work out the ID?

string country = "Australia";
ddlCountry.DataSource = listofcountries;
ddlCountry.DataTextField = "Value";
ddlCountry.DataValueField = "Key";
ddlCountry.DataBind();

ddlCounty.SelectedValue = ???

Thanks

Upvotes: 0

Views: 1113

Answers (1)

Suraj Shrestha
Suraj Shrestha

Reputation: 1808

 ddlCountry.Items.FindByText(country).Selected = true

Upvotes: 2

Related Questions