I have two dropdownlist and a button. I used the breakpoint in my project and everything is working fine. But as soon I am getting out of the function of the button this is
A previous answer mentions ddl.SelectedItem = "parameter";
as an option. However, SelectedItem
is readonly.
Using ddl.SelectedValue = "value"
will also resolve the OP's error.
This code will solve this issue:
YourDropDownId.ClearSelection();
Usually this error occurs when you load your ddl as following:
ddl.FindByValue("parameter").Selected = true;
To overcome this error, you should clear the previous selection of your ddl as following:
ddl.ClearSelection();
ddl.FindByValue("parameter").Selected = true;
Or you can do as following:
ddl.SelectedItem = "parameter";
I hope i could help someone. ;-)
**If you are checking that both dropdownlist selected index should be higher than 0. Then you should check it like if (ddlPlayer1.SelectedIndex>0 || ddlPlayer2.SelectedIndex>0) {}
I think the error comes to use "DropDownList1.SelectedItem.Value".
protected void Button1_Click(object sender, EventArgs e)
{
if (ddlPlayer1.SelectedIndex>0 || ddlPlayer2.SelectedIndex>0)
{
lblPlayer1Score.Text = Repository.Instance.ReturnScore(ddlPlayer1.SelectedValue.ToString(), ddlPlayer2.SelectedValue.ToString()).Rows[0][0].ToString();
lblPlayer2Score.Text = Repository.Instance.ReturnScore(ddlPlayer2.SelectedValue.ToString(), ddlPlayer1.SelectedValue.ToString()).Rows[0][0].ToString();
}
}
Make sure you are not databinding multiple ddls to the same datasource. Being selected is an attribute of an item, therefore, if different ddls select different items from the same datasource, each of the ddls ends up with multiple items selected which is probably what is happening here..