title: ASP.NET dropdownlist is empty in server button_click, the ddl is populated client side
body: <p>I've a form to change some address information. When putting some <code>generic ZIP code</code> in a text box a client event is fired to populate a dropdownlist with compatible cities. </p>
  
  <p>The populate is correct, the problem is no matter which value I select after, because when I click on the button to save the data, that dropdownlist in server function <code>button_click</code> have <code>SelectedItem to null</code>, have aso list Items count to 0.. </p>
  
  <p>I've put another dropdownlist just for test, with some random cities in it, and if I set a value on it by the same jquery function I can correctly see it in the server function, so the problem seems to be with the fact that the other is populated client side.. </p>
  
  <p>I've seen this: <a href="https://stackoverflow.com/questions/24558382/asp-net-restoring-client-side-selecteditem-of-dropdownlist-on-server-side">Asp.Net: Restoring client-side SelectedItem of DropDownList on server-side</a> and the set hidden field value workaround, but I was wondering if there's a more correct way to do this..</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Address&lt;/label&gt;&lt;br /&gt;
              &lt;asp:TextBox ID="txtAddress" CssClass="testo" runat="server"&gt;&lt;/asp:TextBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;label&gt;Province&lt;/label&gt;&lt;br /&gt;
              &lt;asp:TextBox ID="txtProvince" runat="server" /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;label&gt;City&lt;/label&gt;&lt;br /&gt;
              &lt;asp:DropDownList ID="ddlCity" runat="server" /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              &lt;label&gt;ZIP&lt;/label&gt;&lt;br /&gt;
              &lt;asp:TextBox ID="txtZIP" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;!-- Just for test set ddl by jQuery --&gt;
              &lt;asp:DropDownList ID="ddlTest" runat="server" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:ImageButton ID="btnSave" runat="server"
                  OnClick="btnSave_Click" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  
  $(function () {
  
      $(document).on('keyup', 'input[id*="txtZIP"]', function () { 
          var input = $(this);
          if (input[0].selectionStart == 5) {
              $.ajax({ 
                  type: "POST",
                  contentType: "application/json; charset=utf-8",
                  url: "CleanAddress.aspx/txtZIP_TextChanged",
                  data: "{ZIP:'" + input.val() + "'}",
                  dataType: "json",
                  success: function (response) {
                      var cities = $.parseJSON(JSON.stringify(response.d)); 
                      if (comuni.length &gt; 0) {
                          $("[id*=txtProvince]").val(cities[0].ID_PROVINCE);
                          $("[id*='ddlCity']").empty();
                          $.each(cities, function (key, value) {
                              $("[id*='ddlCity']").append($("&lt;option /&gt;
  ").val(value.ID_CITY).text(value.CITY));
                          });
                          $("[id*='ddlCity']").removeAttr('disabled');
                          $("[id*='ddlTest']").val(9); // Just for test
                          $("[id*='ddlComuneDom']").val(cities[0].ID_CITY);
                          //alert($("[id*='ddlTest']").val());
                          //alert($("[id*='ddlCity']").val());
                          }
                      }
                  },
                  error: function (result) {
                      alert("Errore! " + result.status + " - " + result.statusText);
                  }
              });
          }
      });
  
  [WebMethod]
      public static List&lt;Comuni&gt; txtZIP_TextChanged(string ZIP)
      {
          try
          {
              List&lt;Cities&gt; cities = new List&lt;Cities&gt;();
              if ((ZIP.Trim() != "") &amp;&amp; (ZIP.Length == 5))
              {
                  dcListCitiesDataContext dc = new dcListCitiesDataContext();
  
                  if (dc.Cities.Where(c =&gt; c.ZIP.Equals(ZIP)).Count() &gt; 0)
                  {
                      string province = dc.Cities.Where(c =&gt; c.ZIP.Equals(ZIP)).First().ID_PROVINCE.ToUpper();
                      if (province != null)
                      {
                          cities = dc.Cities.Where(c =&gt; c.ZIP.Equals(ZIP)).ToList();
                          return cities;
                      }
  
                  }
              }
              return cities;
          }
          catch (Exception ex)
          {
              Utility.WriteLog("CleanAddress - txtZIP_TextChanged " + " - " + ex.Message, TipoLog.Error);
              throw;
          }
      }
  
      protected void btnSave_Click(object sender, ImageClickEventArgs e)
  {
      // Here I've always ddlCity empty
      string cities= "";
      if (ddlCity.SelectedItem != null)
          cities= ddlCity.SelectedItem.ToString();
      else
          cities= ddlTest.SelectedItem.ToString();
      (...)
  }
  </code></pre>