title: How to nest repeaters in asp.net
body: <p>I need to know how to nest repeaters within a user control. The html side of things is fine, it's the binding and code behind I need help with. I've only been able to find examples using an sql data source, which doesn't really help.</p>
  
  <p>My repeaters look like this:</p>
  
  <pre><code>&lt;asp:Panel ID="pnlDiscipline" runat="server" CssClass=""&gt;
      &lt;asp:Repeater ID="rptDiscipline" runat="server"&gt;
          &lt;ItemTemplate&gt;
              &lt;h4&gt;&lt;%#Eval("Discipline")%&gt;&lt;/h4&gt;
              &lt;ul&gt;
                  &lt;asp:Repeater ID="rptPrograms" runat="server"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;li&gt;&lt;asp:HyperLink runat="server" Text='&lt;%#Eval("Name") %&gt;' NavigateUrl='&lt;%#Eval("Link") %&gt;'&gt;&lt;/asp:HyperLink&gt; &lt;%#Eval ("Notation") %&gt;&lt;/li&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:Repeater&gt;
              &lt;/ul&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p></p>
  
  <p>What I need to do is hopefully reasonably clear - the h4 discipline should appear once, all the entries that belong to the discipline are listed below, then the next h4, then the appropriate list, the next h4 and so on.</p>
  
  <p>The datasource is a dataview created in the codebehind where each row has 'Name', "Link', 'NOtation' and 'Discipline'. I've bound the dataview to the outermost repeater, and it behaves as expected - lists the discipline name for each entry, but shows no data in the inner repeater.</p>
  
  <p>How do I go about making this work?</p>
  
  <p>EDIT: Just to clarify, I have one datatable in the codebehind. Each row in that table is an item, each item belongs to a discipline. I want to use the outer repeater to list the disciplines, the inner to list the items grouped under each discipline. Like so:</p>
  
  <pre><code>&lt;h4&gt;DISCIPLINE 1&lt;/h4&gt;
      &lt;ul&gt;
          &lt;li&gt;Item&lt;/li&gt;
          &lt;li&gt;Item&lt;/li&gt;
          &lt;li&gt;Item&lt;/li&gt;
      &lt;/ul&gt;
  &lt;h4&gt;DISCIPLINE 2&lt;/h4&gt;
      &lt;ul&gt;        
          &lt;li&gt;Item&lt;/li&gt;            
          &lt;li&gt;Item&lt;/li&gt;
      &lt;/ul&gt;
  &lt;h4&gt;DISCIPLINE 3&lt;/h4&gt;
      &lt;ul&gt;        
          &lt;li&gt;Item&lt;/li&gt;            
          &lt;li&gt;Item&lt;/li&gt;
      &lt;/ul&gt;
  </code></pre>
  
  <p>At present, binding the datatable to the outer repeater gives this (example uses the data above):</p>
  
  <pre><code>    &lt;h4&gt;DISCIPLINE 1&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 1&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 1&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 2&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 2&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 3&lt;/h4&gt;
      &lt;h4&gt;DISCIPLINE 3&lt;/h4&gt;
  </code></pre>
  
  <p>I've used OnItemDataBound on the outer repeater as suggested, and as a test case am able to access the data:</p>
  
  <pre><code>protected void rptDiscipline_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
  {
      DataRowView drView = (DataRowView) e.Item.DataItem;
      string name = drView["Name"] as string;
      string link = drView["Link"] as string;
      string notation = drView["Notation"] as string;
      Response.Write(name + link + notation + "&lt;br /&gt;")
  }
  </code></pre>
  
  <p>So the data is there, it is exactly what I would expect to see, I just can't get it bound to the inner repeater. If there is a more performant way to achieve the same, I'm happy to rework my solution.</p>