RG-3
RG-3

Reputation: 6188

Cannot have multiple items selected in a DropDownList

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 the error I am getting:

Cannot have multiple items selected in a DropDownList.

Here is my code to that button:

 protected void Button1_Click(object sender, EventArgs e)
    {

        if (ddlPlayer1.SelectedItem.Value != "0" || ddlPlayer2.SelectedItem.Value != "0" && ddlPlayer1.SelectedItem.Value != ddlPlayer2.SelectedItem.Value)
        {
            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();


        }

        ddlPlayer1.DataBind();
        ddlPlayer2.DataBind();
    }

What I am doing wrong here?

Upvotes: 44

Views: 84044

Answers (8)

alaasdk
alaasdk

Reputation: 1998

This code will solve this issue:

YourDropDownId.ClearSelection(); 

Upvotes: 22

CSwanson
CSwanson

Reputation: 1

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.

Upvotes: 0

peroija
peroija

Reputation: 1982

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.

Upvotes: 1

zkanoca
zkanoca

Reputation: 9928

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

Upvotes: 2

DS Delcio
DS Delcio

Reputation: 1191

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

Upvotes: 119

Rudrik
Rudrik

Reputation: 832

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

Upvotes: 56

RG-3
RG-3

Reputation: 6188

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


        }

    }

Upvotes: 0

Deepak
Deepak

Reputation: 7927

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

Upvotes: -1

Related Questions