title: How to define listview templates in code
body: <p>I am writing a Composite control, which contains a listview to display a table of items. Normally when using a ListView in Asp.NET I would define the templates in the code-forward.</p>
  
  <pre><code>&lt;asp:ListView runat="server" ID="ArticleList"&gt;
      &lt;LayoutTemplate&gt;
          &lt;div class="ContentContainer"&gt;
              &lt;div runat="server" id="itemPlaceholder" /&gt;
          &lt;/div&gt;
      &lt;/LayoutTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;div&gt;
              &lt;div&gt;&lt;%# Eval("Content") %&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/ItemTemplate&gt;
      &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>I assume it's something like:</p>
  
  <pre><code>ListView view = new ListView();
  view.LayoutTemplate = .....
  view.ItemTemplate = .....
  
  // when do I call these?
  view.DataSource = myDataSource;
  view.DataBind();
  </code></pre>
  
  <p><strong>Update:</strong>
  I created 2 templates by implementing the ITemplate interface:</p>
  
  <pre><code>private class LayoutTemplate : ITemplate
  {
      public void InstantiateIn(Control container)
      {
          var outer = new HtmlGenericControl("div");
          var inner = new HtmlGenericControl("div") { ID = "itemPlaceholder" };
          table.Rows.Add(row);
  
          container.Controls.Add(table);
      }
  }
  
  private class ItemTemplate : ITemplate
  {
      public void InstantiateIn(Control container)
      {
          var inner = new HtmlGenericControl("div");
  
          container.Controls.Add(inner);
      }
  }
  </code></pre>
  
  <p>and I can add them using:</p>
  
  <pre><code>dataList.LayoutTemplate = new LayoutTemplate();
  dataList.ItemTemplate = new ItemTemplate();
  </code></pre>
  
  <p>But then I get stuck, since container.DataItem is null.</p>