Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have to assign the control to the form BEFORE setting it's properties.</p> <p>For example:</p> <pre><code>foreach (var car in cars){ Label lbl = new Label (); form1.Controls.Add(lbl); // Create and immediately assign lbl.Text = car.name; // now you can set some properties Image myImage = new Image (); form1.Controls.Add(myImage); // Ditto myImage.ImageUrl = car.imageURL; } </code></pre> <hr> <p><strong>UPDATE:</strong> </p> <p>I just created a brand new web application project with a page that only had a button on it; it worked as expected. I suggest you have something else going on. See below:</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and the code behind:</p> <pre><code>namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { for (Int32 i = 0; i &lt; 10; i++) { Label lbl = new Label(); form1.Controls.Add(lbl); lbl.Text = "HAHAHAHAH"; } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload