title: SELEC2 4.0 with multi select in ASP.Net: How to get and set selected items?
body: <p>I am trying to use SELECT2 in ASP.Net in combination with a dropdown list.</p>
  
  <p>This is my .aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="ASPNet_SELECT2_1._default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;
      &lt;title&gt;a SELECT2 implementation in ASP.NET&lt;/title&gt;
      &lt;script src="scripts/js/jquery-1.12.0.js"&gt;&lt;/script&gt;
      &lt;script src="scripts/js/select2/select2.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="css/select2.css" /&gt;
  
      &lt;script&gt;
              $(document).ready(function () {
                  $("#" + "&lt;%=dd.ClientID%&gt;").select2({
                  placeholder: "Select a Subject",
                  allowClear: true
              });
          });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
              &lt;asp:DropDownList ID="dd" runat="server" ClientIDMode="Static" Width="300px" multiple="multiple"&gt;&lt;/asp:DropDownList&gt;
          &lt;/div&gt;
          &lt;asp:Button ID="btnGetSelected" runat="server" Text="Get selected items" OnClick="btnGetSelected_Click" /&gt;&amp;nbsp;&lt;asp:Button ID="btnSetSelected" runat="server" Text="Set selected items" OnClick="btnSetSelected_Click" /&gt;
          &lt;br /&gt;
          &lt;asp:Literal ID="lit" runat="server"&gt;&lt;/asp:Literal&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my code behind</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace ASPNet_SELECT2_1
  {
      public partial class _default : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!Page.IsPostBack)
              {
                  for (int i = 0; i &lt; 10; i++)
                  {
                      ListItem l = new ListItem("Item " + i.ToString());
                      dd.Items.Add(l);
                  }
              }
          }
  
          protected void btnGetSelected_Click(object sender, EventArgs e)
          {
              lit.Text = dd.Text;
          }
  
          protected void btnSetSelected_Click(object sender, EventArgs e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>The binding of the SELECT2 control with my dropdown list works fine. I also have set the "multiple" attribute for the dropdown list to have the multi select functionality. But i am not able to get the selected items. And also am i not able to set these items.</p>
  
  <p>When i select multiple elements and do a postback (for example on button click), then after the postback only one item is selected. And i always get the selected first item.</p>
  
  <p>How can get all selected items and also set multiple selected items on postback and also via code? </p>