title: add item template to listview with dynamic bindings asp.net
body: <p>I am trying to implement a dynamic listView in ASP.NET. For this the column header text should be generated dynamically from a List and also the data should be binded to the listView from List. The dynamic generation of the column headers works until now. But I have problems to bind the List to the listView.</p>
  
  <p>What I have done until now is the following:</p>
  
  <p>aspx code:</p>
  
  <pre><code>&lt;asp:ListView ID="lvFriends" runat="server"&gt;
  
      &lt;LayoutTemplate&gt; &lt;%-- generated dynamically --%&gt;
      &lt;/LayoutTemplate&gt;
  
      &lt;ItemTemplate&gt;
          &lt;tr&gt;
              &lt;td id="itemTemplate"&gt;
                  &lt;%# Eval("Name")%&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:ListView&gt;
  </code></pre>
  
  <p>code behind:</p>
  
  <pre><code>private class LayoutTemplate : ITemplate
  {
     public List&lt;String&gt; ColumnNames { get; set; }
  
     public LayoutTemplate ()
     {
         ColumnNames = new List&lt;String&gt;();
     }
     private class LayoutTemplate : ITemplate
     {
        ph.Controls.Add(new LiteralControl("&lt;table&gt;"));
        ph.Controls.Add(new LiteralControl("&lt;tr&gt;"));
        foreach (String columnName in ColumnNames)
        {
            ph.Controls.Add(new LiteralControl("&lt;td&gt;" + columnName + "&lt;/td&gt;"));
        }
        ph.Controls.Add(new LiteralControl("&lt;/tr&gt;"));
        ph.Controls.Add(new LiteralControl("&lt;tr&gt;&lt;/tr&gt;") { ID = "ItemPlaceholder" });
        ph.Controls.Add(new LiteralControl("&lt;/table&gt;"));
  
        container.Controls.Add(ph);
      }
  }
  
  private class ItemTemplate : ITemplate
  {
      public void InstantiateIn(Control container)
      {
          PlaceHolder ph = new PlaceHolder();
  
          ph.Controls.Add(new LiteralControl("&lt;tr&gt;&lt;td&gt;")); 
          //ph.Controls.Add(&lt;%# eval(\"Name\")%&gt;));
          ph.Controls.Add(new LiteralControl("&lt;/tr&gt;&lt;/td&gt;"));
  
          container.Controls.Add(ph);
      }
   }
  
   public class Friends
   {
       public string Name { get; set; }
   }
  
   protected void Page_Load(object sender, EventArgs e)
   {
       LayoutTemplate myTemplate = new LayoutTemplate();
       myTemplate.ColumnNames.Add("Name");
       myTemplate.ColumnNames.Add("Date");
       ...
  
       lvFriends.LayoutTemplate = myTemplate;
  
       //lvFriends.ItemTemplate = new ItemTemplate();
  
       List&lt;Friends&gt; dataList;
       dataList = getData();
  
       lvFriends.DataSource = dataList;
       lvFriends.DataBind();
   }
  </code></pre>
  
  <p>Now I want to assign also an itemTemplate to the listView. But I don't know how I can implement "&lt;%# eval(\"Name\")%>" as code!</p>