title: Dynamically Nesting Repeaters ASP.net
body: <p>I have a portion of code where I nest repeaters by hardcoding them in HTML:</p>
  
  <pre><code>    &lt;asp:Repeater ID="rpt_1" runat="server"&gt;
          &lt;ItemTemplate&gt;
              &lt;div&gt;&lt;%# Eval("ID") %&gt;&lt;/div&gt;
               &lt;asp:Repeater ID="rpt_2" runat="server"&gt;
                   &lt;ItemTemplate&gt;
                        &lt;div&gt;&lt;%# Eval("ID") %&gt;&lt;/div&gt;
                        &lt;asp:Repeater ID="rpt_3" runat="server"&gt;
                            &lt;ItemTemplate&gt;
                                &lt;div&gt;&lt;%# Eval("ID") %&gt;&lt;/div&gt;
                            &lt;/ItemTemplate&gt;
                        &lt;/asp:Repeater&gt;
                   &lt;/ItemTemplate&gt;
               &lt;/asp:Repeater&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>Each repeater's ItemTemplate has exactly the same HTML and layout stuff.  Is there a way to programatically nest them n number of repeaters based on the layout of one?  Here's kind of pseduo code of what I want to do:</p>
  
  <pre><code>     &lt;asp:Repeater ID="rpt_1" runat="server"&gt;
          &lt;ItemTemplate&gt;
              &lt;div&gt;&lt;%# Eval("ID") %&gt;&lt;/div&gt;
               &lt;asp:Repeater ID="rpt_2" runat="server"/&gt;                
          &lt;/ItemTemplate&gt;
      &lt;/asp:Repeater&gt;
  
     rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
     rpt_1.DataSource = q;
     rpt_1.DataBind();
  
  protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
  {
      if (sender is Repeater)
      {
          string strID = ((Repeater)sender).ID.ToString();
          strID = strID.Substring(strID.Length - 1, 1).ToString();
          Repeater rpt = (Repeater)e.Item.FindControl("rpt_" + strID);
  
          if (strID != "4")
          {//Stop the nested creation
  
          //Does this work?
          //How do I accomplish this?
          rpt.ItemTemplate = (Repeater)e.Item.FindControl("rpt_" + (Convert.ToInt32(strID)-1).ToString()).ItemTemplate;
  
          rpt_1.ItemDataBound += new RepeaterItemEventHandler(rpt_ItemDataBound);
          rpt.DataSource = q;
          rpt.DataBind();
          }
      }
  }
  </code></pre>
  
  <p>Can I assign the ItemTemplate of one repeater to another in code?</p>