Dropdown list selected value not working
Asked Answered
C

7

12

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.

Convoy answered 3/5, 2012 at 10:14 Comment(3)
ddlcSCountry.SelectedIndex = ddlcSCountry.Items.IndexOf(ddlcSCountry.Items.FindByValue(ddlcBCountry.SelectedItem.Value))Eden
Can you expand on Not working (evident since you posted), do you get any error ? is the dropdown poulated ?Yellowwood
i dint get any error. the dropdownlist is getting no responseConvoy
G
21

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...

Godric answered 3/5, 2012 at 11:52 Comment(6)
Hello. i bind the dropdownlist by using entity data sourceConvoy
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?Godric
ya. i try put a break point within chkSameBAddress_CheckedChanged. The CheckedChanged event is firedConvoy
I tell you what, try this. Comment your both dropdowns in the markup and replace them with <asp:DropDownList ID="ddlcBCountry" runat="server"><asp:ListItem Value="1" Text="Value1"></asp:ListItem> <asp:ListItem Value="2" Text="Value2"></asp:ListItem> </asp:DropDownList> AND <asp:DropDownList ID="ddlcSCountry" runat="server"><asp:ListItem Value="1" Text="Value1"></asp:ListItem> <asp:ListItem Value="2" Text="Value2"></asp:ListItem> </asp:DropDownList> respectively and see if it works. If yes definitely you are rebinding the controls before executing the event.Godric
+1 Thank you, Esteemed Stack Overflow Contributor, for saving me at least an hour of frustration.Austronesian
Awesome! Exactly my issue: binding every page load. Duh, didn't think about postbacks! Thanks again.Immeasurable
B
3

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.

Bullet answered 1/4, 2015 at 3:57 Comment(0)
T
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.

Tangential answered 3/5, 2012 at 10:30 Comment(0)
M
0

Try this for select

ddlcSCountry.Text=ddlcBCountry.SelectedItem.Value;

It will selected needed item

Meryl answered 3/5, 2012 at 10:20 Comment(1)
hi there are not ddlcSCountry.text .Is it ddlcSCountry.SelectedItem.text?Convoy
T
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.

Triciatrick answered 3/5, 2012 at 10:30 Comment(0)
I
0

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

Icicle answered 18/8, 2014 at 22:53 Comment(0)
M
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.

Mcpherson answered 22/6, 2019 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.