title: How do you 'clone' WebControls in C# .NET?
body: <p>My basic question is, in .NET, how do I clone WebControls?</p>
  
  <p>I would like to build a custom tag, which can produce multiple copies of its children.
  Ultimately I intend to build a tag similar to  in JSP/Struts.</p>
  
  <p>But the first hurdle I have is the ability to duplicate/clone the contents of a control.</p>
  
  <p>Consider this rather contrived example;</p>
  
  <pre><code>&lt;custom:duplicate count="2"&gt;
      &lt;div&gt;
          &lt;p&gt;Some html&lt;/p&gt;
          &lt;asp:TextBox id="tb1" runat="server" /&gt;
      &lt;/div&gt;
  &lt;/custom:duplicate&gt;
  </code></pre>
  
  <p>The HTML markup which is output would be something like,</p>
  
  <pre><code>&lt;div&gt;
      &lt;p&gt;Some html&lt;/p&gt;
      &lt;input type="text" id="tb1" /&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;p&gt;Some html&lt;/p&gt;
      &lt;input type="text" id="tb1" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><em>Note: I know i have the id duplicated, I can come up with a solution to that later!</em></p>
  
  <p>So what we would have is my custom control with 3 children (I think) - a literal control, a TextBox control, and another literal control.</p>
  
  <p>In this example I have said 'count=2' so what the control should do is output/render its children twice.</p>
  
  <p>What I would hope to do is write some "OnInit" code which does something like:</p>
  
  <pre><code>List&lt;WebControl&gt; clones;
  for(int i=1; i&lt;count; i++)
  {
      foreach(WebControl c in Controls) 
      {
          WebControl clone = c.Clone();
          clones.Add(clone);
      }
  }
  
  Controls.AddRange(clones);
  </code></pre>
  
  <p>However, as far as I can tell, WebControls do not implement ICloneable, so its not possible to clone them in this way.</p>
  
  <p>Any ideas how I can clone WebControls?</p>