Dropdown list selected value not working
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.
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.
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...