title: Eval/Binding DropDownList in DetailsView
body: <p>I have a dropdownlist inside a detailsview that is getting its values from one table and binding the selected values to a different table.  The problem I am having is that whenever postback occours the table that I am getting the ddl values from is changed back to the default read.  This makes the value that was selected always change to null (which is the first value of the list)</p>
  
  <p>I have tried putting !IsPostBack in the page_load:</p>
  
  <pre><code>   if (!IsPostBack)
     {
          DetailsView1.DataBind();
     }
  </code></pre>
  
  <p>I do have a second ddl that is dependent on the first list but that one is working fine, it is the first list that always is null and wont hold the selected value. </p>
  
  <p>Here is the first ddl:</p>
  
  <pre><code>  &lt;asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" 
      AutoPostBack="True" DataSourceID="SQLLEAVECODE" DataTextField="LEAVETYPE" 
      DataValueField="LEAVECODE" Height="18px" style="text-transform:uppercase;"
      onselectedindexchanged="DropDownList1_SelectedIndexChanged"
      SelectedValue='&lt;%# bind("reqleavecode") %&gt;' Width="145px"&gt;
      &lt;asp:ListItem&gt;&lt;/asp:ListItem&gt;
    &lt;/asp:DropDownList&gt;
  </code></pre>
  
  <p>I can't seem to figure this out, I think it may have something to do with my binding. </p>
  
  <pre><code>    public string lvtype;
      public string lvrequest;
  
      private DataSet GetData()
      {
          ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
  
          var sql = "SELECT LEAVETYPE, LEAVECODE FROM TESTBWTIME.BWLVTYPE ORDER BY LEAVECODE";
  
          using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
          {
              conn.Open();
  
              using (iDB2Command cmd = new iDB2Command(sql, conn))
              {
                  cmd.DeriveParameters();
  
                  using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
                  {
                      DataSet ds = new DataSet();
                      da.Fill(ds);
  
                      return ds;
                  }
              }
          }
      }
  
      private String GetConnectionString()
      {
          ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
  
          return cssc["connStringNET"].ToString();
      }
  
      protected void Page_Load(object sender, EventArgs e)
      {
          DropDownList ddl1 = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
          DropDownList ddl2 = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
          DataSet ds = GetData();
          if (!Page.IsPostBack)
          {
              ddl1.DataSource = ds;
              ddl1.DataBind();
              lvtype = ddl1.SelectedValue;
  
              ddl2.DataSource = ds;
              ddl2.DataBind();
              lvrequest = ddl2.SelectedValue;
          }
          else
          {
              lvtype = ddl1.SelectedValue;
              lvrequest = ddl2.SelectedValue;
          }
         }
  </code></pre>