title: How can I bind data from nested List<> objects to nested ListView controls?
body: <p>So I have an asp.net web page where I display some results from an image recognition service. The results are in the form of an object of class OrpObject (the name is irrelevant), which contains a List&lt;> of type Labels_texture. Each of these Labels_texture objects contains their own List&lt;> of type Labels_color.</p>
  
  <p>What I want to do is use a ListView to display each of the results from the Labels_texture list; and then inside each of those individual elements of the ListView, display another (nested) ListView corresponding to the elements in the nested Labels_color List&lt;> in my OrpObject.</p>
  
  <p>The main catch is that there could be a variable number of elements (or none at all) in any of these lists at any time, since they are results from an image recognition service. <strong>My main question is: how can I bind the data from the inner List&lt;> in my OrpObject called Labels_color to the corresponding inner ListViews in my webpage, without specifying array indexes when setting the DataSource and doing the DataBind() for those inner ListViews?</strong></p>
  
  <p>Hopefully I was able to make some sense out of all that. Here are the relevant code snippets:</p>
  
  <p>The OrpObject:</p>
  
  <pre><code>public class OrpObject
  {
      public List&lt;texture&gt; Labels_texture { get; set; }
  
  }
  
  public class texture
  {
      public string detector { get; set; }
      public string category { get; set; }
      public string matched_url { get; set; }
      public List&lt;color&gt; Labels_color { get; set; }
  }
  
  public class color
  {
      public string category { get; set; }
      public string categoryID { get; set; }
      public string confidence { get; set; }
      public string matched_url { get; set; }
  }
  </code></pre>
  
  <p>And here is the ListView Code from my .aspx file (trimmed down for brevity) (NOTE: I'm new to ASP.NET and I have been blindly attaching runat="server" to everything in my LayoutTemplate just in case it needs to be there. If I don't need all that, could you let me know?)</p>
  
  <pre><code>&lt;asp:ListView ID="ListView_Orp_Results" runat="server"&gt;
       &lt;LayoutTemplate&gt;
          &lt;div id="outer_result_container" runat="server"&gt;
          &lt;div id="itemPlaceholder" runat="server"&gt;
              &lt;div id="result_photo" runat="server"&gt;
              &lt;/div&gt;
              &lt;div id="result_category" runat="server"&gt;
              &lt;/div&gt;
              &lt;div id="result_detector" runat="server"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;div id="inner_result_container" runat="server"&gt;
              &lt;asp:ListView ID="ListView_inner_results" runat="server"&gt;
                  &lt;LayoutTemplate&gt;
                  &lt;div id="outer_result_container" runat="server"&gt;
                  &lt;div id="itemPlaceholder" runat="server"&gt;
                      &lt;div id="inner_result_photo" runat="server"&gt;
                      &lt;/div&gt;
                      &lt;div id="inner_result_category" runat="server"&gt;
                      &lt;/div&gt;
                      &lt;div id="inner_result_categoryID" runat="server"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;/LayoutTemplate&gt;
                  &lt;ItemTemplate&gt;
                      ...
                  &lt;/ItemTemplate&gt;
                  &lt;EmptyDataTemplate&gt;
                      ...
                  &lt;/EmptyDataTemplate&gt;
                  &lt;EmptyItemTemplate&gt;
                      ...
                  &lt;/EmptyItemTemplate&gt;
              &lt;/asp:ListView&gt;
          &lt;/div&gt;
       &lt;/LayoutTemplate&gt;
       &lt;ItemTemplate&gt;
          ...
       &lt;/ItemTemplate&gt;
       &lt;EmptyDataTemplate&gt;
          ...
      &lt;/EmptyDataTemplate&gt;
      &lt;EmptyItemTemplate&gt;
          ...
      &lt;/EmptyItemTemplate&gt;
  &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>Finally, here is the snippet from my code-behind file where I try to set my OrpObject and the corresponding Lists to the ListViews' DataSources:</p>
  
  <pre><code>ListView_Orp_Results.DataSource = myOrp.Labels_texture;
  ListView_Orp_Results.DataBind();
  
  foreach (texture myTexture in myOrp.Labels_texture)
  {
      ListView_inner_results.DataSource = myTexture.Labels_color;
      ListView_inner_results.DataBind();
  }
  </code></pre>
  
  <p>I know a foreach loop here probably won't work... But it's the only thing I could think of to make sure I generate a nested ListView for every texture object contained in the Labels_texture list...</p>
  
  <p>Sorry for the novel, and thanks in advance for any input you might be able to offer!</p>