Cannot have multiple items selected in a DropDownList

Starr Duskk
Starr Duskk used Ask the Experts™
on
I'm using a select2 multiple dropdownlist. I am getting the above error.
I've seen posts where they say this error occurs if you have two items in the list with the same value, but I do not have that and I also use the "multiple" option.

    <select name = "ctl00$ContentBody$TeamProfileControl$ddlDistrictId" id="ddlDistrictId" Class="ddlSelectMultiple" name="ddlDistrictId" multiple="multiple" style="width:100%;">
    	<option value = "1786" > AP_Test_District</Option>
    	<option value = "209" > Honey Dew South</Option>
    	<option value = "562" > HQ</Option>
    	<option value = "38" > JK&#39;s Crib</Option>
    	<option value = "206" > Kansas City Office</Option>
    	<option value = "759" > Testing District</Option>

    </select>

Open in new window


I also read to add items this way, however, I populate with a sql databind, so using this is not applicable:
DDLModality.Items.Add(Li1)

I need to preselect the previously saved values in the dropdownlist to display them as selected on the screen. Here is my code, it runs with no errors but when the screen is loaded it generates:
Cannot have multiple items selected in a DropDownList.

   Public Sub CheckMultiListBoxNumeric(ByRef ddlDistrictId As DropDownList, ByVal districtIds As String)

        ddlDistrictId.ClearSelection()

        Dim checkedItems As String()
        checkedItems = Split(districtIds, ",")
        Dim selected As List(Of String) = checkedItems.ToList()

        For Each row As String In selected


            If ddlDistrictId.Items.FindByValue(row) IsNot Nothing Then
                ddlDistrictId.Items.FindByValue(row).Selected = True
            End If


        Next

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
Cannot have multiple items selected in a DropDownList.

I would try use a CheckboxList control instead in order to have multiple items selected.
Starr DuskkASP.NET VB.NET Developer

Author

Commented:
dropdown list with multi select
yet, I can use a dropdownlist with multiple selections and save them just fine.

I only need to know how to populate them again on entering the page with the previously saved values.

I see some solutions, but they are using javascript/jquery. I wanted to do this in codebehind.
Ryan ChongSoftware Team Lead

Commented:
if your control is runat = server, it should be easy enough to retrieve its values back in code behind
Starr DuskkASP.NET VB.NET Developer

Author

Commented:
that wasn't the problem. I could retrieve just fine.

I said:
>> I can use a dropdownlist with multiple selections and save them just fine.

I changed it to a listbox:
                                <asp:ListBox runat="server" ID="lstZoneId"  name="lstZoneId" ClientIDMode="Static"></asp:ListBox>
And it pulls fine.
ASP.NET VB.NET Developer
Commented:
I changed it to a listbox:
                                <asp:ListBox runat="server" ID="lstZoneId"  name="lstZoneId" ClientIDMode="Static"></asp:ListBox>
And it pulls fine.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial