user3020047 user3020047 - 2 years ago 120 Question

In, after setting a dropdownlist with a text value, how to then get the selected value

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


I get 1 when I want 2.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download