title: SelectedValue property and dynamically created DropDownLists. ASP.NET
body: <p>The .aspx file has it's usual html code and a "Panel1" control in it.
  First of all i'm creating a dropdownlist called "ddl" and add to it some ListItems, all this from the code behind. Next i want to create a certain number of other DropDownLists and copy to them all the ListItems I added to the "ddl", after that i need them to be added to a "Panel1" control when the page is running. The most important part is that I want all the dynamically created dropdownlists to have a selected value when running the page. You can see the code below:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      DropDownList ddl = new DropDownList();
      ddl.Items.Add(new ListItem("One", "1"));
      ddl.Items.Add(new ListItem("Two", "2"));
      ddl.Items.Add(new ListItem("Three", "3"));
      ddl.Items.Add(new ListItem("Four", "4"));
      ddl.Items.Add(new ListItem("Five", "5"));
      ddl.Items.Add(new ListItem("Six", "6"));
      ddl.Items.Add(new ListItem("Seven", "7"));
  
      int j = 2;
      for (int h = 0; h &lt; 3; h++)
      {
          DropDownList ddlDynamic = new DropDownList();
  
          //Add the items from ddl to the new dropdownlsit
          for (int i = 0; i &lt; ddl.Items.Count; i++)
          {
              ddlDynamic.Items.Add(ddl.Items[i]);
          }
          //the selected item in the first dropdownlist 
          //must be "Two" but it will be "Four". WHY???
          ddlDynamic.SelectedValue = j.ToString();
          ddlDynamic.ID = h.ToString();
          Panel1.Controls.Add(ddlDynamic);
          Panel1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
          j++;
      }
  }
  </code></pre>
  
  <p>The problem here is that the selected value for all three dropdownlists will be the same and it will be "Four", when logically the first must be "Two", second "Three" and the third must has "Four" as selected value. First question is: What am I doing wrong?</p>
  
  <p>Second question.
  When using <code>ddlDynamic.Items.FindByValue(j.ToString()).Selected = true;</code> instead of <code>ddlDynamic.SelectedValue = j.ToString();</code> I am getting a "Cannot have multiple items selected in a DropDownList." Why is that?</p>
  
  <p>Thank You.</p>