title: ASP.NET jQuery double listbox edit and save
body: <p>Okay - so I have been pulling out my hair on this one for far too long now. Hopefully you guys can help out. </p>
  
  <p>I have a form that allows a user to input/edit data about their company. Most of it is pretty straight-forward form field editing, but I have one part where I am allowing the user to select what state(s) their business operates in and then assign a contact to that state. I am using jQuery and 2 listBox controls where I allow the user to double-click the state(s) they want to operate in. The jQuery moves the selected state over to the other listBox as well as to a dropdown control where they can then enter their state-specific contact information.</p>
  
  <p>Now for the "hair pulling out" part. I have seen a lot of posts about taking the data from the 2nd listBox control, putting it into an array and placing it into a hidden form field - but the example of how to do this are few and far between. The few that I have found I have not successfully integrated. Any help there would by much appreciated.</p>
  
  <p>The other piece of the puzzle is that this page is supposed to function as a page the user can edit as well - so on the initial load my page should (ideally) preload the state(s) a user has already selected via jQuery into the second listBox...but the states that go into the second listBox should NOT show up in the first. Ugh. Any pointers as to how to accomplish this would also be greatly appreciated.</p>
  
  <p>Here is my code:</p>
  
  <p>ASPX file:</p>
  
  <pre><code>&lt;%@ Page Title="Mappings" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Mapping.aspx.cs" Inherits="Account_Mapping" EnableEventValidation="false" %&gt;
  
  &lt;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"&gt;
  &lt;script language="javascript" type="text/javascript"&gt;
      $(function () {
          var sourceState = $('#MainContent_sourceState option').clone();
  
          $('#stateAllA').click(function () {
              $('#MainContent_sourceState option').appendTo('#MainContent_targetState, #MainContent_contactState');
          });
  
          $('#stateAllR').click(function () {
              $('#MainContent_targetState option').appendTo('#MainContent_sourceState');
              $('#MainContent_contactState option').remove();
          });
  
  
  
          $('#MainContent_sourceState').dblclick(function () {
              $('#MainContent_sourceState option:selected').appendTo('#MainContent_targetState, #MainContent_contactState');
          });
  
  
          $('#MainContent_targetState').dblclick(function () {
              var targetList = $('#MainContent_targetState option:selected');
              var targetVal = $('#MainContent_targetState option:selected').val();
              targetList.appendTo('#MainContent_sourceState');
              $('#MainContent_contactState option[value=' + targetVal + ']').remove();
              //$('#contactState option:selected').remove();
              var foption = $('#MainContent_sourceState option:first');
              var soptions = $.makeArray($('#MainContent_sourceState option:not(:first)')).sort(function (a, b) {
                  return a.text == b.text ? 0 : a.text &lt; b.text ? -1 : 1
              });
              $('#MainContent_sourceState').html(soptions).prepend(foption);
              foption.attr("selected", true).siblings("option").removeAttr("selected");
          });
  
      });
  &lt;/script&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt;
      &lt;h2&gt;
          Admin
      &lt;/h2&gt;
      &lt;p&gt;
      Please fill in all the required information and click "submit" when complete.
      &lt;/p&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Company Name:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Name" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Company Phone:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Phone" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Main Contact Person:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Contact" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Main Email Address:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Email" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Company Website:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Website" runat="server" CssClass="textEntry"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Company Description:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Desc" runat="server" CssClass="textAreaEntry" TextMode="MultiLine"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;Company Access:&lt;/b&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;asp:TextBox ID="comp_Access" runat="server" CssClass="textAreaEntry" TextMode="MultiLine"&gt;&lt;/asp:TextBox&gt;
      &lt;/div&gt;
      &lt;hr /&gt;
      &lt;p&gt;
      &lt;b&gt;Which state(s) do you operate in?&lt;/b&gt;
      &lt;/p&gt;
      &lt;div class="formLeft"&gt;
          &lt;b&gt;OPTIONS:&lt;/b&gt; &lt;a id="stateAllA" href="#" style="font-size:11px" onclick="return false;"&gt;(add all)&lt;/a&gt;&lt;br /&gt;
          &lt;em style="font-size:12px"&gt;(double-click an item to add it to your list)&lt;/em&gt;&lt;br /&gt;
          &lt;asp:ListBox ID="sourceState" runat="server" CssClass="selectEntry" SelectionMode="Multiple" DataSourceID="myDSID" DataTextField="state_name" DataValueField="state_ID"&gt;&lt;/asp:ListBox&gt;
          &lt;asp:SqlDataSource ID="myDSID" runat="server" 
              ConnectionString="&lt;%$ ConnectionStrings:myDatasource %&gt;" 
              SelectCommand="SELECT [state_ID], [state_name] FROM [states]"&gt;
          &lt;/asp:SqlDataSource&gt;
      &lt;/div&gt;
      &lt;div class="formRight"&gt;
          &lt;b&gt;YOUR SELECTIONS:&lt;/b&gt; &lt;a id="stateAllR" href="#" style="font-size:11px" onclick="return false;"&gt;(remove all)&lt;/a&gt;&lt;br /&gt;
          &lt;em style="font-size:12px"&gt;(double-click an item to remove it from your list)&lt;/em&gt;&lt;br /&gt;
          &lt;asp:ListBox ID="targetState" runat="server" CssClass="selectEntry" SelectionMode="Multiple"&gt;&lt;/asp:ListBox&gt;
          &lt;asp:HiddenField ID="hdnStates" runat="server" /&gt;
      &lt;/div&gt;
      &lt;p&gt;
      &lt;b&gt;Do you wish to assign state-specific contacts? If so, choose the appropriate state and fill out the info below.&lt;/b&gt;&lt;br&gt;
      &lt;/p&gt;
      &lt;p&gt;
      &lt;asp:DropDownList ID="contactState" runat="server"&gt;&lt;/asp:DropDownList&gt;
      &lt;/p&gt;
      &lt;div align="right" style="margin-top:5px;"&gt;
          &lt;asp:Button ID="Button1" runat="server" Text="Submit" 
              onclick="Button1_Click" /&gt;
      &lt;/div&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>And my CodeBehind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  public partial class Account_Mapping : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          //let's get our passed variables
          //the company you're editing comes from a dropdown on prev page
          string passedComp = Session["searchCompany"].ToString();
  
          if (!IsPostBack)
          {
              //now let's get all the relevant data from the DB
  
              //first, the easy stuff
              DataTable dt1 = GetBasicCompData(passedComp);
  
              //let's populate those fields in the form
              comp_Name.Text = dt1.Rows[0][0].ToString();
              comp_Phone.Text = dt1.Rows[0][1].ToString();
              comp_Contact.Text = dt1.Rows[0][2].ToString();
              comp_Email.Text = dt1.Rows[0][3].ToString();
              comp_Website.Text = dt1.Rows[0][4].ToString();
              comp_Desc.Text = dt1.Rows[0][5].ToString();
              comp_Access.Text = dt1.Rows[0][6].ToString();
  
              //state mapping
              DataTable dt2 = GetStateCompData(passedComp);
  
              for (int i = 0; i &lt; dt2.Rows.Count; i++)
              {
                  //targetState.DataSource = dt2;
                  //targetState.DataValueField = "state_ID";
                  //targetState.DataTextField = "state_Name";
                  //targetState.DataBind();
  
                  //contactState.DataSource = dt2;
                  //contactState.DataValueField = "state_ID";
                  //contactState.DataTextField = "state_Name";
                  //contactState.DataBind();
              }
          }
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          //breakpoint here to try and return my values 
          string myStates = hdnStates.Value;
      }
  
  
      public DataTable GetBasicCompData(string strComp)
      {
          //Query DB based on result
          string strConn = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
          SqlConnection con = new SqlConnection(strConn);
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = con;
          cmd.CommandType = System.Data.CommandType.Text;
          cmd.Parameters.AddWithValue("@Comp", strComp);
          cmd.CommandText = "SELECT companies.comp_Name, companies.comp_Phone, companies.comp_Contact, companies.comp_Email, companies.comp_Website, companies.comp_Desc, companies.comp_Access FROM companies WHERE comp_ID =  ''+ @Comp +''";
          DataSet objDs = new DataSet();
          SqlDataAdapter dAdapter = new SqlDataAdapter();
          dAdapter.SelectCommand = cmd;
          con.Open();
          dAdapter.Fill(objDs);
          con.Close();
          return objDs.Tables[0];
      }
  
      public DataTable GetStateCompData(string strComp)
      {
          //Query DB based on result
          string strConn = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
          SqlConnection con = new SqlConnection(strConn);
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = con;
          cmd.CommandType = System.Data.CommandType.Text;
          cmd.Parameters.AddWithValue("@Comp", strComp);
          cmd.CommandText = "SELECT states.state_ID, states.state_Name, contact_Name, contact_Phone, contact_Email FROM states INNER JOIN companies_states ON states.state_ID = companies_states.state_ID WHERE comp_ID = ''+ @Comp +''";
          DataSet objDs = new DataSet();
          SqlDataAdapter dAdapter = new SqlDataAdapter();
          dAdapter.SelectCommand = cmd;
          con.Open();
          dAdapter.Fill(objDs);
          con.Close();
          return objDs.Tables[0];
      }
  }
  </code></pre>
  
  <p>So there you have it. It's a lot to digest, I know. I'd love a kick in the right direction. :)</p>