11

In my ASP.NET project. I have two dropdownlist and a checkbox. When the checkbox is checked, the selected value of DropDownList1 must be same as selcted value of the DropDownList2. But the DropDownList1.SelectedValue is not working.

Here is my code:

protected void chkSameBAddress_CheckedChanged(object sender, EventArgs e)
{
    try
    {
        if (this.chkSameBAddress.Checked == true)
        {

          this.txtcSAddress1.Text=  this.txtcBAddress1.Text;
          this.txtcSAddress2.Text = this.txtcBAddress2.Text;
          this.txtcSAddress3.Text = this.txtcBAddress3.Text;
          this.txtcSAddress4.Text = this.txtcBAddress4.Text;
          this.txtcSCity.Text = this.txtcBCity.Text;
          this.txtcSPostCode.Text = this.txtcBPostCode.Text;
          this.txtcSState.Text = this.txtcBState.Text;

          this.ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value).Selected = true;


        }

    }
    catch (Exception ex)
    {
        logger.Error(ex.Message);
        throw;

    }
}

As seen in the example above, if chkSmaeBAddress is checked then the selected value of ddlcSCountry must be same as ddlcBCountry selected value.

Mixcels
  • 829
  • 1
  • 10
  • 23
user998405
  • 1,309
  • 3
  • 40
  • 80

7 Answers7

19

Where are you binding data to these dropdown list controls? They should be bound only in the initial loading of the page as follows. I suspect that you are binding them in every page load and therefore selected values disappear.

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        //Please check if you are binding checkbox controls here. 
        //If not bring them in here
    }
}

Other condition is that both ddlcSCountry and ddlcBCountry hould have same values to be able to select. Otherwise ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value) will be null and will throw an error when trying to set the Selected property

If both above conditions are okay, your code should work.

EDIT Sorry, my commented code should be to check binding of dropdown list controls not the checkbox. so it should be as

//Please check if you are binding both dropdown list controls here. 
//If not bind them within the if (!Page.IsPostBack)

Put a breakpoint in your if (this.chkSameBAddress.Checked == true) line within CheckedChanged event and see it is executing and then the runtime values...

Kaf
  • 30,825
  • 7
  • 51
  • 74
  • Hello. i bind the dropdownlist by using entity data source – user998405 May 03 '12 at 12:42
  • You can put a break point as I said above and check runtime values. Nothing wrong with your code but I suspect the problem is the order in which you have done it. Is your `chkSameBAddress_CheckedChanged` event firing at all? – Kaf May 03 '12 at 12:51
  • ya. i try put a break point within chkSameBAddress_CheckedChanged. The CheckedChanged event is fired – user998405 May 03 '12 at 12:54
  • I tell you what, try this. Comment your both dropdowns in the markup and replace them with ` ` AND ` ` respectively and see if it works. If yes definitely you are rebinding the controls before executing the event. – Kaf May 03 '12 at 13:13
  • 1
    +1 Thank you, Esteemed Stack Overflow Contributor, for saving me at least an hour of frustration. – Dan Solovay Oct 30 '12 at 22:14
  • Awesome! Exactly my issue: binding every page load. Duh, didn't think about postbacks! Thanks again. – Vippy Mar 14 '14 at 03:10
2

Surely you are trying to make the dropdown boxes equal?

use

ddlcSCountry.SelectedIndex = ddlcSCountry.FindStringExact(ddlcBCountry.Text);

This will select the matching option in the list and not just set the text in the field, which is very useful when you have underlying values with your text options.

IAmGroot
  • 13,301
  • 18
  • 72
  • 146
2

The accepted solution is an obvious solution to the most common cause, however, there is one more surprising issue that can cause this!

My list values came from a database and the values had linefeed and carriage return from the database values: \r\n. These values look like an innocent space, but actually they are not!

My solution was to remove these hidden Char values. Hope it helps.

Nicholas
  • 10,490
  • 19
  • 67
  • 85
0

Try this for select

ddlcSCountry.Text=ddlcBCountry.SelectedItem.Value;

It will selected needed item

Likurg
  • 2,726
  • 15
  • 21
0

Make sure that chkSameBAddress.AutoPostBack is set to true. If it is set and still doesn't work, consider using an UpdatePanel control or moving that logic to the client using JavaScript.

npclaudiu
  • 2,259
  • 1
  • 14
  • 17
0

Make sure you have AutoPostBack set to true in the properties of the DropDownList.

bmich72
  • 560
  • 4
  • 6
0

I just switch to using <select runat="server" id="test1"></Select> I only had to make slight modifications the code behind and it all worked better.