title: ASP.NET find control by id
body: <p>I am doing a simple web site using asp.net and i am having troubles finding my  or  objects by id in the code behind in c#. I have something like this:</p>
  
  <pre><code>&lt;asp:ListView ID="InternamentosListView" runat="server"
              DataSourceID="InternamentosBD"&gt;
          &lt;LayoutTemplate&gt;
              &lt;table id="camas"&gt;
  
                      &lt;asp:PlaceHolder runat="server" ID="TablePlaceHolder"&gt;&lt;/asp:PlaceHolder&gt;
  
              &lt;/table&gt;
  
          &lt;/LayoutTemplate&gt;
  </code></pre>
  
  <p>the rest is irrelevant, and then i use this in the code behind:</p>
  
  <pre><code>Table table = (Table)FindControl("camas");
  </code></pre>
  
  <p>i also tried:</p>
  
  <pre><code>Table table = (Table)camas;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Control table= (Table)FindControl("camas");
  </code></pre>
  
  <p>and each one of this lines gives me Null. am i doing something wrong ?</p>
  
  <p>EDIT: From your answers i did this:</p>
  
  <pre><code>&lt;LayoutTemplate&gt;
  
              &lt;table id="camas" runat="server"&gt;
  
  
              &lt;/table&gt;
  
          &lt;/LayoutTemplate&gt;
  </code></pre>
  
  <p>and tried all the things stated above. same result.</p>
  
  <p>EDIT2: Whole Code:</p>
  
  <p>C#</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
  
          Table table = (Table)FindControl("camas");
          HiddenField NCamasHF = (HiddenField)FindControl("NCamas");
          int NCamas = Convert.ToInt32(NCamasHF);
          HiddenField NColunasHF = (HiddenField)FindControl("NColunas");
          HiddenField CorMasc = (HiddenField)FindControl("CorMasc");
          HiddenField CorFem = (HiddenField)FindControl("CorFem");
          int NColunas = Convert.ToInt32(NColunasHF);
          TableRow Firstrow = new TableRow();
          table.Rows.Add(Firstrow);
          for (int i = 1; i &lt;= NCamas; i++)
          {
              if (i % NColunas == 0)
              {
                  TableRow row = new TableRow();
                  table.Rows.Add(row);
                  TableCell cell = new TableCell();
                  cell.BackColor = System.Drawing.Color.LightGreen;
                  cell.CssClass = "celula";
                  row.Cells.Add(cell);
              }
              else
              {
                  TableCell cell = new TableCell();
                  cell.BackColor = System.Drawing.Color.LightGreen;
                  cell.CssClass = "celula";
                  Firstrow.Cells.Add(cell);
              }
          }
      }
  </code></pre>
  
  <p>asp.net</p>
  
  <pre><code>&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;
  
       &lt;asp:SqlDataSource
          ID="ParametrosBD"
          ConnectionString ="&lt;%$ ConnectionStrings:principal %&gt;"
          ProviderName = "System.Data.SqlClient"
          SelectCommand = "SELECT par_Id, par_NCamas, par_NColunas, par_CorMasculino, par_CorFeminino FROM Parametros WHERE par_Id=1"
          runat="server"&gt;
         &lt;/asp:SqlDataSource&gt;
      &lt;asp:ListView ID="InternamentosListView" runat="server"
              DataSourceID="InternamentosBD"&gt;
          &lt;LayoutTemplate&gt;
  
              &lt;table id="camas" runat="server"&gt;
  
  
              &lt;/table&gt;
  
          &lt;/LayoutTemplate&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:HiddenField ID="NCamas" runat="server" Value='&lt;%# Bind("par_NCamas") %&gt;' /&gt;
              &lt;asp:HiddenField ID="NColunas" runat="server" Value='&lt;%# Bind("par_NColunas") %&gt;' /&gt;
              &lt;asp:HiddenField ID="CorMasc" runat="server" Value='&lt;%# Bind("par_CorMasculino") %&gt;' /&gt;
              &lt;asp:HiddenField ID="CorFem" runat="server" Value='&lt;%# Bind("par_CorFeminino") %&gt;' /&gt;
              &lt;tr id="cama"&gt;&lt;/tr&gt;
              &lt;/ItemTemplate&gt;
      &lt;/asp:ListView&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>