1

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.

4
  • It is always helpful to add the particular technology you are using. ;) Commented Apr 16, 2016 at 3:15
  • I did mention vb.net. Is a dropdownlist different in a winform vs a asp.net page for more clarity? Commented Apr 16, 2016 at 3:22
  • No, I mean I just added asp.net tag to your question if you notice. Commented Apr 16, 2016 at 3:22
  • OK..thanks for that. Commented Apr 16, 2016 at 3:23

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.