title: Dynamic Repeater in C#.net
body: <p>I am trying to create nested repeaters dynamically using ITemplate. This repeater is like that I am passing <code>List&lt;Control&gt;</code> and it generates repeater. Issue is that when I databound outer repeater. Only the last nested repeater shows. Following is screen shot. 
  <a href="https://i.stack.imgur.com/PaT4Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PaT4Y.png" alt="screen shot"></a>
  </p>
  
  <p><strong>Markup</strong> </p>
  
  <hr>
  
  <pre><code>    &lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Restricted_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;table style="border:solid 1px black;"&gt;
          &lt;tr&gt;
              &lt;td&gt;
                  &lt;asp:PlaceHolder ID="phControls" runat="server" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Code Behind</strong></p>
  
  <hr>
  
  <pre><code>
  using System;
  using System.Collections.Generic;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  
  public partial class Restricted_Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          CreateNestedRepeater();
      }
  
      private void CreateNestedRepeater()
      {
          Repeater childRpt = new Repeater();
          List repeatingRuleControls = new List();
          repeatingRuleControls.Add(new TextBox());
          repeatingRuleControls.Add(new TextBox());
          repeatingRuleControls.Add(new TextBox());
          RepeatingRuleTemplate repeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);
          childRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);
          childRpt.ItemTemplate = repeatingRuleTemplate;
          childRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);
          childRpt.DataSource = new DataRow[4];
  
          Repeater parentRpt = new Repeater();
          repeatingRuleControls = new List();
          repeatingRuleControls.Add(new TextBox());
          repeatingRuleControls.Add(new TextBox());
          repeatingRuleControls.Add(new TextBox());
          repeatingRuleControls.Add(childRpt);
          RepeatingRuleTemplate parentrepeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);
          parentRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);
          parentRpt.ItemTemplate = parentrepeatingRuleTemplate;
          parentRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);
          parentRpt.DataSource = new DataRow[4];
          parentRpt.DataBind();
          phControls.Controls.Add(parentRpt);
      }
  
      public class RepeatingRuleTemplate : ITemplate
      {
          ListItemType templateType;
          List innerControls;
  
          public RepeatingRuleTemplate(ListItemType type, List controls)
          {
              templateType = type;
              innerControls = controls;
          }
  
  
  
          public void InstantiateIn(Control container)
          {
              PlaceHolder ph = new PlaceHolder();
  
              switch (templateType)
              {
                  case ListItemType.Header:
                      ph.Controls.Add(new LiteralControl(""));
                      ph.Controls.Add(new LiteralControl(""));
                      foreach (Control control in innerControls)
                      {
                          Label label = new Label();
                          label.Text = control.ID;
                          ph.Controls.Add(new LiteralControl(""));
                          ph.Controls.Add(label);
                          ph.Controls.Add(new LiteralControl(""));
                      }
                      ph.Controls.Add(new LiteralControl(""));
                      break;
                  case ListItemType.Item:
                      ph.Controls.Add(new LiteralControl(""));
  
                      foreach (Control control in innerControls)
                      {
                          if (control.GetType() != typeof(Repeater))
                          {
                              ph.Controls.Add(new LiteralControl(""));
                              TextBox textBox = new TextBox();
                              textBox.ID = control.ID;
                              ph.Controls.Add(textBox);
                              ph.Controls.Add(new LiteralControl(""));
                          }
                          else
                          {
                              ph.Controls.Add(new LiteralControl(""));
                              ph.Controls.Add(control as Repeater);
                              //(control as Repeater).DataSource = new DataRow[4];
                              //   (control as Repeater).DataBind();
                              ph.Controls.Add(new LiteralControl(""));
                          }
                      }
                      ph.Controls.Add(new LiteralControl(""));
                      //ph.DataBinding += new EventHandler(Item_DataBinding);
                      break;
                  case ListItemType.Footer:
                      ph.Controls.Add(new LiteralControl(""));
                      break;
              }
              container.Controls.Add(ph);
          }
  
  
  
          public List Controls
          {
              get
              {
                  return innerControls;
              }
          }
  
      }
  }
  </code></pre>
  
  <hr>