'add item template to listview with dynamic bindings asp.net
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.
What I have done until now is the following:
aspx code:
<asp:ListView ID="lvFriends" runat="server">
<LayoutTemplate> <%-- generated dynamically --%>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td id="itemTemplate">
<%# Eval("Name")%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
code behind:
private class LayoutTemplate : ITemplate
{
public List<String> ColumnNames { get; set; }
public LayoutTemplate ()
{
ColumnNames = new List<String>();
}
private class LayoutTemplate : ITemplate
{
ph.Controls.Add(new LiteralControl("<table>"));
ph.Controls.Add(new LiteralControl("<tr>"));
foreach (String columnName in ColumnNames)
{
ph.Controls.Add(new LiteralControl("<td>" + columnName + "</td>"));
}
ph.Controls.Add(new LiteralControl("</tr>"));
ph.Controls.Add(new LiteralControl("<tr></tr>") { ID = "ItemPlaceholder" });
ph.Controls.Add(new LiteralControl("</table>"));
container.Controls.Add(ph);
}
}
private class ItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
PlaceHolder ph = new PlaceHolder();
ph.Controls.Add(new LiteralControl("<tr><td>"));
//ph.Controls.Add(<%# eval(\"Name\")%>));
ph.Controls.Add(new LiteralControl("</tr></td>"));
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<Friends> dataList;
dataList = getData();
lvFriends.DataSource = dataList;
lvFriends.DataBind();
}
Now I want to assign also an itemTemplate to the listView. But I don't know how I can implement "<%# eval(\"Name\")%>" as code!
Solution 1:[1]
To implement evaluation of data item inside your item template you need to actually get access to the data item. Data item is held by the container control, so first step is to cast it to the more specific data bound item control. In case of ListView that is ListViewItem:
ListViewItem item = (ListViewItem)container;
Now data item is available via simple:
item.DataItem
To evaluate value of some specific field use DataBinder.Eval (this is the same thing ASP.NET actually uses when you write <%# Eval() %>):
DataBinder.Eval(item.DataItem, "Name")
Now everything is straightforward, just produce the necessary markup:
PlaceHolder ph = new PlaceHolder();
ph.Controls.Add(new LiteralControl("<tr><td>"));
ph.Controls.Add(new LiteralControl(DataBinder.Eval(item.DataItem, "Name") as string));
ph.Controls.Add(new LiteralControl("</td></tr>"));
item.Controls.Add(ph);
The above snippet can actually be simplified a lot:
LiteralControl itemMarkup =
new LiteralControl("<tr><td>"
+ (DataBinder.Eval(item.DataItem, "Name") as string)
+ "</td></tr>");
item.Controls.Add(itemMarkup);
For the sake of completeness, whole ItemTemplate class:
public class ItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
ListViewItem item = (ListViewItem)container;
LiteralControl itemMarkup =
new LiteralControl("<tr><td>"
+ (DataBinder.Eval(item.DataItem, "Name") as string)
+ "</td></tr>");
item.Controls.Add(itemMarkup);
}
}
Of course 2 years is a bit late to help the OP, but perhaps this answer will help future seekers.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrei |
