How to fill the second dropdown with the same value as the first dropdown

98 views Asked by At

I have 2 databounded dropdownlist. They have the same value(all months).

I want that, if a user choose (in the first Dropdown) for example april then the second Dropdown should jump automatically to April.

The only thing I could think of was:

protected void ddMonthfrom_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddMonthfrom.SelectedItem.Text = ddMonthto.SelectedItem.Text;
    }

It does not work. When I click on April the second Dropdown does not Change.

3

There are 3 answers

0
Karan On BEST ANSWER

try ddMonthto.SelectedValue = ddMonthfrom.SelectedItem.Value;

  • OnFristDropDownSelectedIndexChange (ddMonthfrom)
  • Set the value of the Second DropDown (ddMonthto).
0
Sorceri On

If they are the same values in both Drop Downs then remove the text portion

ddMonthfrom.SelectedItem = ddMonthto.SelectedItem;
0
41686d6564 On

Try:

ddMonthfrom.SelectedIndex = ddMonthfrom.FindStringExact(ddMonthto.Text);

This should also work if the comboBox ddMonthto doesn't have a DataSource.

Notes:

  • You can also use .SelectedItem.ToString() instead of .Text, while .SelectedItem.Text isn't correct.
  • If you populate your comboBox using a datasource, you can just use the same datasource for both comboBoxes, and the same item should be selected automatically.

Hope that helps :)