Get Selected value from multiple value in dropdownlist Select2

This example is totally what I'm looking for

And after choose all the countries needed, I will click Submit button. My problem is on how to get all the selected value from ddl1.

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<script type="text/javascript" src=""></script>
<link rel="stylesheet" href="" />
<script type="text/javascript" src=""></script>
<link href="" rel="stylesheet" type="text/css" />
<script src="" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        placeholder: "Select",
        allowClear: true

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    Upload Attachment
    <br /><br />

    <asp:Label ID="Label2" runat="server" Text="Attachment :  " class="form-control"></asp:Label>&nbsp;&nbsp;
    <asp:FileUpload ID="FileUpload1" runat="server" class="form-control"/>
    <br /><br />

    <asp:Label ID="Label1" runat="server" Text="Plase select Staff No :  " class="form-control"></asp:Label>&nbsp;&nbsp;
    <asp:DropDownList ID="ddl1" Width="300px" runat="server" multiple="multiple" CssClass="form-control js-example-placeholder-single" ToolTip="Select " OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
    <br /><br />

    <asp:Button ID="btnUpload" runat="server" Text="Upload Attachment" OnClick="btnUpload_Click"/>

    <br /><br />

And this is C# code behind

private void PopulateDropDownList()
    String strConnString = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "sp_User";
    cmd.Connection = con;

        ddl1.DataSource = cmd.ExecuteReader();
        ddl1.DataTextField = "IDName";
        ddl1.DataValueField = "StaffNo";

    catch (Exception ex)
        throw ex;

protected void btnUpload_Click(object sender, EventArgs e)
    string message = "";
    string rnonow = DateTime.Now.ToString("yyyyMMdd_hhmmss");
    rnoUploaded = rno;

    Label4.Text = ddl1.SelectedValue; //this is where i'm stuck on how to call the ddl1 selected value. This code just only get the 1st selected value only eg : Brazil only not the other 2 below.
    //below is my trial to get the selected value but fail
    //foreach (ListItem item in ddl1.Items)
    //    if (item.Selected)
    //    {
    //       insert statement here

    //    }

Please help. Thank you

enter image description here

2 answers

  • answered 2021-02-21 21:06 Arib Yousuf

    You can't use DropDownList for multi-select. Use ListBox e.g.

    <asp:ListBox ID="ddl" runat="server" SelectionMode="Multiple" CssClass="form-control js-example-placeholder-single" ToolTip="Select">

    In code-behind:

    foreach (ListItem item in ddl.Items)
        if (item.Selected)
           var text = item.Text;
           var value = item.Value;

  • answered 2021-02-22 01:08 Dia

    I got the answer by creating a field, and jqeury store the data into it.

    $('[id*=ddl1]').on('change', function () {
    <asp:HiddenField ID="hfSelected" runat="server" />