简体   繁体   中英

how to save all the values selected in dropdownlist

I have a dropdownlist that I used some JS to to change it and I used this site (I used the multiple selected2): link

With this I wanted to get all the values selected in the drop downlist and insert them into my database but for that I have to get all the values selected but I only get 1 of them.

asp.net

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="MenuBar.css" />
    <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <title></title>
</head>
<body style="background-color: #fff">
    <form id="form1" runat="server">
        <div class="principal">
            <div class="txtbox">
                <div class="rz-fieldset-content">
                    <asp:Button ID="ButtonInsert" runat="server" Text="Inserir" OnClick="ButtonInsert_Click" />
                    <asp:Button ID="ButtonChange" runat="server" Text="Alterar" OnClick="ButtonChange_Click" />
                    <fieldset id="fieldset" runat="server">
                        <legend>Encomendas</legend>
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="Label1" runat="server" Style="display: block; color: black; font-size: 15px;" Text="Selecione o BL"></asp:Label>
                                </td>
                                <td>
                                    <asp:DropDownList ID="DropDownListBL" CssClass="txt" Style="padding-right: 10px; color: grey;" runat="server">
                                        <asp:ListItem Text="- -" />
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="Label2" runat="server" Style="display: block; color: black; font-size: 15px;" Text="Selecione a(s) encomenda(s)"></asp:Label>
                                </td>
                                <td>
                                    <asp:DropDownList ID="multiple" CssClass="txt" Style="padding-right: 10px; color: grey; width: 100%" runat="server" multiple>

                                    </asp:DropDownList>
                                </td>
                            </tr>
                        </table>
                    </fieldset>
                    <asp:Button ID="ButtonCreate" runat="server" Text="Create" OnClick="ButtonCreate_Click" />

                    <asp:Button ID="ButtonBack" runat="server" Text="Back" OnClick="ButtonBack_Click" />
                    <br />
                    <asp:Label ID="Changed" runat="server" Text="Alterado com sucesso!"></asp:Label>
                </div>
            </div>
        </div>
        <!-- jQuery -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <!-- Select2 -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
        <script>
            $("#DropDownListBL").select2({
                /*placeholder: "Select a programming language",*/
                allowClear: true
            });
            $("#multiple").select2({
                /*placeholder: "Select a programming language",*/
                allowClear: true
            });
        </script>
    </form>
</body>
</html>

cs

 protected void ButtonCreate_Click(object sender, EventArgs e)
        {
            string teste = multiple.SelectedValue;
            con.Open();
            if(DropDownListBL.SelectedIndex != 0 && multiple.SelectedIndex != 0)
            {
                SqlCommand cmd = new SqlCommand("UpdtEncomenda", con);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@BL", DropDownListBL.SelectedValue));
                cmd.Parameters.Add(new SqlParameter("@No_", multiple.SelectedValue));
                int value = cmd.ExecuteNonQuery();
                ButtonBack.Visible = true;
                Changed.Visible = true;
                multiple.SelectedIndex = 0;
                DropDownListBL.SelectedIndex = 0;
            }
            con.Close();
        }

Try this to select multiple values:

<asp:ListBox ID="lstEmployee" runat="server" SelectionMode="Multiple">  
<asp:ListItem Text="Nikunj Satasiya" Value="1" />  
<asp:ListItem Text="Dev Karathiya" Value="2" />  
<asp:ListItem Text="Hiren Dobariya" Value="3" />  
<asp:ListItem Text="Vivek Ghadiya" Value="4" />  
<asp:ListItem Text="Pratik Pansuriya" Value="5" />  
</asp:ListBox>  

Read this article https://www.c-sharpcorner.com/blogs/multiselect-dropdownlist-with-checkboxes-in-asp-net-with-c-sharp-and-vb-net-using-jquery-plugin

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM