title: ASP.NET Web Forms DropDownList has a SelectedValue which is invalid because it does not exist in the list of items
body: <p>First of all there has been questions ( <a href="https://stackoverflow.com/questions/1869150/dropdownlist-has-a-selectedvalue-which-is-invalid-because-it-does-not-exist-in-th">DropDownList has a SelectedValue which is invalid because it does not exist in the list of items</a> , <a href="https://stackoverflow.com/questions/5704651/dropdownlist-has-a-selectedvalue-which-is-invalid-because-it-does-not-exist-in-t">DropDownList &quot;has a SelectedValue which is invalid because it does not exist in the list of items&quot;</a> , <a href="https://stackoverflow.com/questions/38612/aspdropdownlist-error-dropdownlist1-has-a-selectedvalue-which-is-invalid-beca">asp:DropDownList Error: &#39;DropDownList1&#39; has a SelectedValue which is invalid because it does not exist in the list of items</a> ) about this and there are proposed workarounds but my question is really WHY this happens. What is more I am not satisfied with the suggested workarounds and I find them quite ugly.</p>
  
  <p>So there is a page with a drop down list and a button:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" ViewStateMode="Disabled" %&gt;
  
  &lt;html lang="en" &gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;asp:DropDownList ID="ddlTest" runat="server"&gt;
          &lt;/asp:DropDownList&gt;
          &lt;asp:Button Text="Test" ID="btnTest" runat="server" onclick="btnTest_Click" /&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I bind ddlTest with some items on Page_Init and then in btnTest_Click I bind again:</p>
  
  <pre><code>using System;
  
  namespace TestWebApplication
  {
      public partial class WebForm2 : System.Web.UI.Page
      {
          protected void Page_Init(object sender, EventArgs e)
          {
              //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
              ddlTest.DataSource = new[] { 1, 2, 3 };
              ddlTest.DataBind();
              ddlTest.SelectedValue = "3";
          }
  
          protected void btnTest_Click(object sender, EventArgs e)
          {
              //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3}
              ddlTest.ClearSelection();
              //SelectedIndex is 0, SelectedValue is "1", SelectedItem is {1}
              ddlTest.SelectedIndex = -1; //Nothing changes including SelectedIndex
              ddlTest.SelectedValue = ""; //Nothing changes including SelectedValue
              ddlTest.Items.Clear();
              //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
              ddlTest.DataSource = null; //Nothing changes except for the DataSource property
              ddlTest.DataSource = new[] { 1, 2 };
              ddlTest.DataBind();//Exception!
              //'ddlTest' has a SelectedValue which is invalid because it does not exist in the list of items.
              //Parameter name: value
          }
      }
  }
  </code></pre>
  
  <p>Why do I get the exception. I tried different versions of these and none of them works. I tried using only ClearSelection but still got the same exception. Is this bug in the control or something I miss. Are the ugly workarounds from the other questions the only solution?</p>
  
  <p>Note - the bug is reproduceable even if the button is removed and the all the code is moved in a single event handler. Just bind once set selected value and bind again.</p>