title: asp.net Can't find controls added from code-behind in a div
body: <p>I'm trying to access Controls in div but I can't find them.</p>
  
  <p>I got a div with some controls in it.</p>
  
  <pre><code>&lt;div id="divChampsFiltrageProjetsTest" class="divChampsFiltrageProjetsTest" runat="server"&gt;
  
    &lt;span runat="server" style="color:black;"&gt; Filtre 1 : &lt;/span&gt;
    &lt;asp:DropDownList ID="ddlFiltreProjets1" runat="server" ForeColor="Black" &gt;
      &lt;asp:ListItem Value="no" Selected="True"&gt;No. Projet&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="desi"&gt;Designation Projet&lt;/asp:ListItem&gt;
      &lt;asp:ListItem Value="chef"&gt;Chef de Projet&lt;/asp:ListItem&gt;
    &lt;/asp:DropDownList&gt;
  
    &lt;asp:TextBox ID="txtValeurFiltreProjets1" runat="server" ForeColor="Black" /&gt;
  
    &lt;asp:Button ID="btnAddFiltre1" runat="server" Text="Ok" OnClick="btnAppliquerFiltreProjets_Click"/&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>I can find these controls in the code behind. But the problem comes when I add some controls from code behind.</p>
  
  <pre><code>            HtmlGenericControl span = new HtmlGenericControl("span");
              DropDownList ddlList = new DropDownList();
              TextBox txtValeur = new TextBox();
              Button btnAdd = new Button();
              ListItem item1 = new ListItem("No. Projet", "no");
              ListItem item2 = new ListItem("Désignation Projet", "desi");
              ListItem item3 = new ListItem("Chef de Projets", "chef");
  
              span.Style.Add(HtmlTextWriterStyle.Color, "Black");
              span.InnerText = "Filtre " + (i + 1).ToString() + " : ";
  
              ddlList.ID = "ddlFiltreProjets" + (i + 1).ToString();
              ddlList.Items.Add(item1);
              ddlList.Items.Add(item2);
              ddlList.Items.Add(item3);
  
              txtValeur.ID = "txtValeurFiltreProjets" + (i + 1).ToString();
              txtValeur.ForeColor = System.Drawing.Color.Black;
  
              btnAdd = btnAddFiltre1;
  
              divChampsFiltrageProjetsTest.Controls.Add(span);
              divChampsFiltrageProjetsTest.Controls.Add(ddlList);
              divChampsFiltrageProjetsTest.Controls.Add(txtValeur);
              divChampsFiltrageProjetsTest.Controls.Add(btnAdd);
  </code></pre>
  
  <p>This part of the code add the controls to the div and when I got the page, controls are in the source code.</p>
  
  
                  
              
  
  <pre><code>        &lt;div id="MainContent_divChampsFiltrageProjets" class="divChampsFiltrageProjets" style="display:block;"&gt;
              &lt;div id="MainContent_divChampsFiltrageProjetsTest" class="divChampsFiltrageProjetsTest"&gt;
  
                  &lt;span style="color:black;"&gt; Filtre 1 : &lt;/span&gt;
                  &lt;select name="ctl00$MainContent$ddlFiltreProjets1" id="MainContent_ddlFiltreProjets1" style="color:Black;"&gt;
  &lt;option value="no"&gt;No. Projet&lt;/option&gt;
  &lt;option value="desi"&gt;Designation Projet&lt;/option&gt;
  &lt;option selected="selected" value="chef"&gt;Chef de Projet&lt;/option&gt;
  
  &lt;/select&gt;
  
                  &lt;input name="ctl00$MainContent$txtValeurFiltreProjets1" type="text" value="johan" id="MainContent_txtValeurFiltreProjets1" style="color:Black;" /&gt;
  
              &lt;span style="color:Black;"&gt;Filtre 2 : &lt;/span&gt;&lt;select name="ctl00$MainContent$ddlFiltreProjets2" id="MainContent_ddlFiltreProjets2"&gt;
  &lt;option value="no"&gt;No. Projet&lt;/option&gt;
  &lt;option value="desi"&gt;D&amp;#233;signation Projet&lt;/option&gt;
  &lt;option value="chef"&gt;Chef de Projets&lt;/option&gt;
  
  &lt;/select&gt;&lt;input name="ctl00$MainContent$txtValeurFiltreProjets2" type="text" id="MainContent_txtValeurFiltreProjets2" style="color:Black;" /&gt;&lt;input type="submit" name="ctl00$MainContent$btnAddFiltre1" value="Ok" id="MainContent_btnAddFiltre1" /&gt;&lt;/div&gt;
  </code></pre>
  
  <p>But when I try to get the controls that I add from code behind it says that the value is null.</p>
  
  <pre><code>    for (int i = 1; i &lt; Convert.ToInt32(Session["nbFiltres"].ToString()) + 1; i++)
      {
  
          DropDownList ddl = (DropDownList)divChampsFiltrageProjetsTest.FindControl("ddlFiltreProjets"+i.ToString());
          TextBox txt = (TextBox)divChampsFiltrageProjetsTest.FindControl("txtValeurFiltreProjets" +i.ToString());
  
          Session["typeFiltreProjets" + i.ToString()] = ddl.SelectedValue.ToString();
          Session["valeurFiltreProjets" + i.ToString()] = txt.Text;
      }
  </code></pre>
  
  <p>This part is in the click event fonction of a button.</p>
  
  <p>So my problem is that the controls that I add from code behind are not really in the div.</p>
  
  <p>Is anyone know why?</p>