We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to get selected values from code behind?

I can get the indexes of the selected items, but I want to get the values of the selected items.  How do I get the values?

code behind:
        //this gets the index of the selected items
        foreach (int item in brandSelect.SelectedItems)
        {

        }

aspx:
<ej:DropDownList ID="brandSelect" runat="server" DataTextField="Brands" ShowCheckbox="true"></ej:DropDownList></span>

3 Replies

ES Ezhil S Syncfusion Team June 4, 2015 12:07 PM UTC

Hi Megan,

Thank you for contacting Syncfusion support.

In order to get the Selected items value of a DropDownList control in code behind on a post back, we suggest you to use the Text property of the DropDownList control. In code behind you can get all the selected items value separated with a delimiter(,), if you want to get the items individually then using Split() method for DropDownList.Text property will return you a collection of selected item values separately. Please refer the following code snippet,
<code>
[ASPX]

    <ej:DropDownList ID="brandSelect" runat="server" DataIdField="ID" DataTextField="Text" ShowCheckbox="true"></ej:DropDownList>

    <br />

    Selected value after post back: <br />

    <asp:Label runat="server" ID="lbl" ></asp:Label><br /> <%--label value updated after post back--%>

<asp:Button Text="PostBack" runat="server" ID="btn1" OnClick="btn1_Click" />
[CodeBehind.cs]

        protected void btn1_Click(object sender, EventArgs e)

        {

            lbl.Text = "";

            foreach (string val in brandSelect.Text.Split(','))

            {

                lbl.Text += val+" , "; // set the label text with selected items value

            }
        }
</code>

Download the sample prepared from the link below:
http://www.syncfusion.com/downloads/support/forum/119307/ze/DropDown_f119307-1200685286

Please let us know if you have any other queries.

Regards,
Ezhil S



MB megan brown June 4, 2015 03:36 PM UTC

Thanks.  Since I want the value and the not the text, I changed your code to:

        foreach (string val in brandSelect.Value.Split(','))
        {
            //do something
        }

And this worked.


ES Ezhil S Syncfusion Team June 5, 2015 05:25 AM UTC

Hi Megan,

Thanks for the update.

We are glad that the suggested solution worked to met with your requirement.

Please let us know if you have any other queries.

Regards,
Ezhil S

Loader.
Live Chat Icon For mobile
Up arrow icon