Reputation: 888
I populate a ddlFrequency dropdownlist from a table, set the text and now just want to get it's corresponding value.
The drop down list - 2 entries:
immediate 1
daily 2
I set the drop down as default to daily.
ddlFrequency.SelectedItem.Text = "Daily"
How to I get the value?
If I do
ddlFrequency.SelectedItem.Value.ToString().
I get 1 when I want 2.
Upvotes: 1
Views: 6918
Reputation: 73731
This line of code:
ddlFrequency.SelectedItem.Text = "Daily";
does not select the item with the value 2
. Instead, it modifies the text of the currently selected item (by default, it would be the first item), setting it to "Daily". Both items would then have the same text, but different values.
You can select the item with the text "Daily" this way:
ddlFrequency.Items.FindByText("Daily").Selected = true;
or, as I prefer to do, set the selected value:
ddlFrequency.SelectedValue = "2";
which can then be retrieved with the same property:
string value = ddlFrequency.SelectedValue;
Upvotes: 1