Cannot have multiple items selected in a DropDownList

后端 未结 8 832
暖寄归人
暖寄归人 2020-12-30 19:11

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

相关标签:
8条回答
  • 2020-12-30 19:46

    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.

    0 讨论(0)
  • 2020-12-30 19:51

    This code will solve this issue:

    YourDropDownId.ClearSelection(); 
    
    0 讨论(0)
  • 2020-12-30 19:52

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

    0 讨论(0)
  • 2020-12-30 19:53

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

    0 讨论(0)
  • 2020-12-30 20:05
     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();
    
    
            }
    
        }
    
    0 讨论(0)
  • 2020-12-30 20:06

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

    0 讨论(0)
提交回复
热议问题