title: Retrieving values from dynamically created controls Label in asp .net
body: <p>I have created Label controls dynamically on button click:</p>
  
  <pre><code>protected void createDynamicLabels_Click(object sender, EventArgs e)
  {
      int n = 5;
      for (int i = 0; i &lt; n; i++)
      {
          Label MyLabel = new Label();
          MyLabel.ID = "lb" + i.ToString();
          MyLabel.Text = "Labell: " + i.ToString();
          MyLabel.Style["Clear"] = "Both";
          MyLabel.Style["Float"] = "Left";
          MyLabel.Style["margin-left"] = "100px";
  
          Panel1.Controls.Add(MyLabel);
      }
  }
  </code></pre>
  
  <p>When I tried to read back fro another button I see Label Control returned null  </p>
  
  <blockquote>
    <p><strong>Label str = (Label)Panel1.FindControl("lb" + i.ToString());</strong></p>
  </blockquote>
  
  <p>not sure what went wrong here </p>
  
  <pre><code>protected void bReadDynValue_Click(object sender, EventArgs e)
  {
  
    int n = 5;
      for (int i = 0; i &lt; n; i++)
      {
          Label str = (Label)Panel1.FindControl("lb" + i.ToString());
          lbGetText.Text = str.Text;
      }
  
  
  }
  </code></pre>