title: On Button click, dropdown loses its value
body: <p>I have two dropdowns. First is for State and second is one for the City. When I select the state, its respective city gets populated in the next dropdown. I have one option to add more cities through textbox on button click while selecting the Other option from the city dropdownlist. </p>
  
  <p>The issue is that whenever i am adding the city from the textbox, the it gets inserted into the respective state and also the dropdownlist loses its value. I tried debugging but couldn't find the exact issue.</p>
  
  <p>Please see the code for your reference:</p>
  
  <pre><code>protected void btnAddDropDown_Click1(object sender, EventArgs e)
  {
      using (SqlConnection con = new SqlConnection(constring))
      {
          con.Open();
          SqlCommand cmd = new SqlCommand();
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.CommandText = "Add_CityforLocation";
          cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 0;
          cmd.Parameters.Add("@CountryName", SqlDbType.VarChar).Value = "India";
          cmd.Parameters.Add("@CityName", SqlDbType.VarChar).Value = txtOtherCity.Text.Trim();
          cmd.Parameters.Add("@StateName", SqlDbType.VarChar).Value = ddllocation1.SelectedItem.ToString();
          cmd.Connection = con;
          try
          {
              cmd.ExecuteNonQuery();
              BindContrydropdown();
          }
          catch (Exception ex)
          {
              Response.Write(ex.Message);//You Can Haave Messagebox here
          }
          finally
          {
              con.Close();
          }
      }
  }
  
  protected void BindContrydropdown()
  {
      //conenction path for database
      //string connection = WebConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
      using (SqlConnection con = new SqlConnection(constring))
      {
          con.Open();
          SqlCommand cmd = new SqlCommand("Select Id,CityName From Career.Location", con);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          da.Fill(ds);
          ddllocation1.DataSource = ds;
          ddllocation1.DataTextField = "CityName";
          ddllocation1.DataValueField = "Id";
          ddllocation1.DataBind();
          ddllocation1.Items.Insert(0, new ListItem("--Select--", "0"));
          ddllocation1.Items.Insert(1, new ListItem("--OTHER--", "0"));
          con.Close();
      }
  }
  </code></pre>
  
  <p>Also, see the HTML for the same:-</p>
  
  <pre><code> &lt;tr&gt;
          &lt;td class="td"&gt;Location/State&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:DropDownList CssClass="txtfld-popup" ID="ddllocation1" OnSelectedIndexChanged="ddllocation1_SelectedIndexChanged" runat="server" AutoPostBack="true"&gt;&lt;/asp:DropDownList&gt;
              &lt;asp:RequiredFieldValidator CssClass="error_msg" ID="RequiredFieldValidator1" ControlToValidate="ddllocation1" runat="server" ErrorMessage="Please enter location" InitialValue="--Select--" SetFocusOnError="true"&gt;&lt;/asp:RequiredFieldValidator&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="td"&gt;Location/City&lt;/td&gt;
          &lt;td&gt;
              &lt;asp:DropDownList CssClass="txtfld-popup" ID="ddlLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"&gt;&lt;/asp:DropDownList&gt;
              &lt;asp:RequiredFieldValidator CssClass="error_msg" ID="reqLocation" ControlToValidate="ddlLocation" runat="server" ErrorMessage="Please enter location" InitialValue="--Select--" SetFocusOnError="true"&gt;&lt;/asp:RequiredFieldValidator&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:TextBox ID="txtOtherCity" runat="server" Visible="false" CssClass="txtfld-popup"&gt;&lt;/asp:TextBox&gt;
              &amp;nbsp;&amp;nbsp;&amp;nbsp;
                      &lt;asp:Button ID="btnAddDropDown" runat="server" Width="63" Text="Add" CausesValidation="false" OnClick="btnAddDropDown_Click1" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  </code></pre>