title: dynamically filled DropDownList does not retain value on postback
body: <p>I have a form which is made to "Create a new profile". My problem is about DropDownLists.</p>
  
  <p>The first DropDown dynamically populate the second one in function of its value.</p>
  
  <p>see this picture:</p>
  
  <p><a href="http://image.noelshack.com/fichiers/2013/22/1369819471-picture-help.png" rel="nofollow noreferrer">http://image.noelshack.com/fichiers/2013/22/1369819471-picture-help.png</a></p>
  
  <p>And the following picture:</p>
  
  <p><a href="http://image.noelshack.com/fichiers/2013/22/1369830738-help2.png" rel="nofollow noreferrer">http://image.noelshack.com/fichiers/2013/22/1369830738-help2.png</a></p>
  
  <p>You can see that my 2nd ddl ("Fonction") is correctly filled BUT when I click on the submit button, the value becomes the null value ("Sélectionnez...") and so my RequiredFieldValidator makes the page not valid!</p>
  
  <p>It seems like my 2nd DropDownList is bounded on every postback even if it's not because of SelectedIndexChanged of my 1st DropDownList.
  The SelectedIndexChanged of the 1st DropDownList is always called on postback and so it throws "populateDdl()" at every PostBack (if a value is selected).</p>
  
  <p>When I click on submit button, it registers a blank value in my database.</p>
  
  <p>What am I missing?</p>
  
  <p>Aspx code:</p>
  
  <pre><code>&lt;asp:DropDownList ID="ddlTypePN" runat="server" DataSourceID="SqlTypePN" EnableViewState="true" 
          DataTextField="libelle" DataValueField="valeur" AutoPostBack="true" OnSelectedIndexChanged="ddlTypePN_SelectedIndexChanged" 
          OnDataBound="ddlTypePN_DataBound" &gt; &lt;/asp:DropDownList&gt; 
  
  &lt;asp:DropDownList runat="server" ID="ddlFctPN" AppendDataBoundItems="false" OnDataBound="ddlFctPN_DataBound" &gt; &lt;/asp:DropDownList&gt; 
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          ddlTypeProf.DataBind(); // don't care
          ddlSsoSrc.DataBind(); // don't care
          ddlTypePN.DataBind(); // The ddl that populate my 2nd ddl
      }
  }
  
  protected void ddlTypePN_SelectedIndexChanged(object sender, EventArgs e)
  {
      string type = ddlTypePN.SelectedValue.ToString().Trim();
      // if PNT
      if (type.ToUpper().Trim().Equals("PNT"))
      {               
          ddlFctPN.Enabled = true;
          ddlTypeAv.Enabled = true;
          rfvTypeAv.Enabled = true;
          populateDdl();
  
      }
      else if (type.ToUpper().Trim().Equals("PNC"))
      {                
          ddlFctPN.Enabled = true;
          ddlTypeAv.Enabled = false;
          rfvTypeAv.Enabled = false;
          populateDdl();
      }     
  }
  
  void populateDdl()
  {
      string val = "fct"+ddlTypePN.SelectedValue.ToString().Trim(); // Used for SELECT
      SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["My_DB"].ConnectionString);
      ddlFctPN.Items.Clear();
      DataTable subjects = new DataTable();
      try
      {
          SqlDataAdapter adapter = new SqlDataAdapter("My SELECT", sqlConn);
          adapter.Fill(subjects);
  
          ddlFctPN.DataSource = subjects;
          ddlFctPN.DataTextField = "libelle";
          ddlFctPN.DataValueField = "valeur";
          ddlFctPN.DataBind();
      }
      catch (Exception ex)
      {
          lblErr.Text = ex.Message;
      }
      ddlFctPN.Items.Insert(0, new ListItem("Sélectionnez...", "null"));
  }
  </code></pre>