title: how to access the dynamically created HTML control from code behind in asp.net
body: <p>i have a webpage, wherein i am using a simple foreach loop and creating table structure, wherein i am displaying an image in td, the requirement is i want to access the image created in td, from code-behind on page load.</p>
  
  <p>but i am unable to find the control, i have made the control as runat='server' and also setted the id.</p>
  
  <pre><code>foreach (DataRow r in dtLogic.TopStories(Convert.ToInt32(ConfigurationManager.AppSettings["TopArticles"]), Convert.ToInt32(ConfigurationManager.AppSettings["PortalId"])).Rows)
          {
              i++;
              str.Append("&lt;table cellpadding='0' cellspacing='0' class='newsItem'&gt;");
              str.Append("&lt;tr&gt;");
              str.Append("&lt;td&gt;&lt;img runat='server' id='img"+i+"' class='thumb' src='" + GetAppSettingValue("MainSiteUrl") + "" + r["image"].ToString() + "' width='128' height='73'&gt;&lt;/td&gt;");
              str.Append("&lt;td&gt;&lt;p class='newsTitle'&gt;&lt;a href='" + r["URL"].ToString() + "'&gt;" + r["title"].ToString() + "&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;");
              str.Append("&lt;td&gt;&lt;img class='arrow' src='/images/arrow.png'&gt;&lt;/td&gt;");
              str.Append("&lt;/tr&gt;");
              str.Append("&lt;/table&gt;");
          }
  </code></pre>
  
  <p>now when i access this image from code behind like below..</p>
  
  <pre><code>HtmlImage img = (HtmlImage)this.FindControl("img1");
  </code></pre>
  
  <p>i gets the NullReference for the img object..</p>
  
  <p>can anyone tell me why its not getting the image, which is runat='server' and rendering on the page.. </p>