Cannot have multiple items selected in a DropDownList

后端 未结 8 833
暖寄归人
暖寄归人 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 20:07

    I was trying to add two other list items to to top of the DropDownList's list after reading data into the DropDownList.

    One of the item was "please pick one...", and the second one was "Not listed here...". So I have created a list item:

    ListItem li1 = new ListItem("please pick one...", "999");
    ListItem li2 = new ListItem("not listed here...", "555");
    

    Then I have tried to add these two ListItems to the three DropDownList. After that I have encountered the same error.

    After creating new ListItem instances for each DropDownList, the problem has gone away...

    0 讨论(0)
  • 2020-12-30 20:13

    Found another way to get the error:

            ddlFromBudget.Items.Clear();
    
            ListItem newItem = new ListItem();
            newItem.Text = "Not Set";
            newItem.Value = "0";
            ddlFromBudget.Items.Add(newItem);
    
            if (ddlB1.SelectedValue.ToString() != "0")
            {
                newItem = new ListItem();
                newItem.Text = ddlB1.SelectedItem.ToString();
                newItem.Value = "1";
                ddlFromBudget.Items.Add(newItem);
            }
    

    The line ddlFromBudget.Items.Add(newItem); sets newItem.Selected = True. Without the line newItem = new ListItem();, you get the error because the selected flag is now true on both items added to the ddl.

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