title: How to create an array of User Controls programmatically
body: <p>How do I programmatically create an array of web user controls?</p>
  
  <p>I created a web user control.  I already know how to implement one of them coding in the aspx file, however I would like to know if it is possible to do that from the code behind in either the Page_Init() or the Page_Load() event.</p>
  
  <p>I already know the initialization on the aspx page.</p>
  
  <pre><code>&lt;%@ Register TagPrefix="uc" TagName="myUsercontrol1" Src="/Controls/myUsercontrol1.ascx" %&gt;
  </code></pre>
  
  <p>In the traditional way, I would do something like:</p>
  
  <pre><code>&lt;div id="divMyusercontrols" runat="server"&gt;
      &lt;uc:myUsercontrol1 id="ctlTheControl" runat="server" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>What I was wanting to do, which does not work, as I tried, is:</p>
  
  <p>In the aspx file:</p>
  
  <pre><code>&lt;div id="divMyusercontrols" runat="server"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In the code behind in let us say the Page_Init() event the following:</p>
  
  <pre><code>String strControl = "&lt;uc:myUsercontrol1 id="ctlTheControl{0}" runat="server" /&gt;";
  String strHtml = null;
  for (int i = 0; i &lt; 5; i++)
       strHtml += String.Format(strControl, i.ToString());
  this.divMyusercontrols.InnerHTML = strHtml;
  </code></pre>
  
  <p>This code sadly does not work.  I realize that I can simply do that manually, but I do not know the actual count.  I will know that ahead of time.</p>
  
  <p>UPDATE (to show answer #3 proper code):</p>
  
  <p>The C# for the aspx file is the following.  I have a call to a static C# code behind, which returns the count.  I then set a property to indicate which item to show and then the for-loop.  Cool!</p>
  
  <pre><code>&lt;%int iCount = MyProject.Items;%&gt;
  &lt;%for (int iIndex = 0; iIndex &lt; iCount; iIndex++)%&gt;
  &lt;%{ %&gt;
      &lt;div runat="server"&gt;
      &lt;uc:myUsercontrol1 id="ctlItem&lt;%=iIndex %&gt;" runat="server" /&gt;
      &lt;/div&gt;
  &lt;%}%&gt;
  </code></pre>
  
  <p>SOLUTION (2013-02-12):
  I tried out all three answers below, sadly after I marked one as a solution.  The winning one is a modified version of the Page_LoadControl().  Here is the code that works, and yes, I ran the code.  Everything works.</p>
  
  <pre><code>// Load an array of controls onto a predefined panel.
  for (int iIndex = 0; iIndex &lt; 10; iIndex++)
  {
      // Load the control.
      MyProject.Controls.MyControl ctlItem = (MyProject.Controls.MyControl)Page.LoadControl(@"/Controls/MyControl.ascx");
  
      // Initialize the control.
      ctlItem.MyIndex = iIndex;
  
      // Add the control to the panel.
      this.pnlItems.Controls.Add(ctlItem);
  }
  </code></pre>
  
  <p>Here is the fixed aspx code.</p>
  
  <pre><code>&lt;div id="divItems" runat="server" class="divItems"&gt;
      &lt;dx:ASPxPanel ID="pnlItems" runat="server" Width="200px"&gt;
      &lt;/dx:ASPxPanel&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I tried doing, MyProject.Controls.MyControl ctlItem = new MyProject.Controls.MyControl(), however that does not work.  I got a null excemption.  Loading the control worked.</p>
  
  <p>The answer, which I too hastilly marked as a solution, does not work.   When ran the designer complained.  Here is the code from the designer.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// ctlPurchases&lt;%=iIndex %&gt; control.
      /// &lt;/summary&gt;
      /// &lt;remarks&gt;
      /// Auto-generated field.
      /// To modify move field declaration from designer file to code-behind file.
      /// &lt;/remarks&gt;
      protected global::MyProject.Controls.MyControl ctlItem&lt;%=iIndex %&gt;;
  </code></pre>
  
  <p>The designer was unhappy about the &lt;%...%> part.  This solution has other problems.  The cleanest one is using the Page_LoadControl.</p>