by DanM
26. January 2010 14:30
Didn’t find this in any easily searchable place, so here’s one for reference. Consider a scenario where you’re binding a templated control to an IEnumerable<string> object. In my case, I needed to display a list of the keys in my cache.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvwKeys.DataSource = AllCacheKeys();
lvwKeys.DataBind();
}
}
private IEnumerable<string> AllCacheKeys()
{
IDictionaryEnumerator enumerator = HttpContext.Current.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
yield return enumerator.Key.ToString();
}
}
So how do I reference the string values in my template? <%#Eval(“xyz”) %> expects xyz to be the property of the object being bound to, but a simple string has no property for its value. I’ve seen <%#Eval(“.”) %> suggested else place where . is supposed to refer to the object as whole, but this does not work here. Instead, we must use <%# Container.DataItem %> to refer to the object itself.
<asp:ListView runat="server" ID="lvwKeys">
<LayoutTemplate>
<table class="form">
<tbody>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Container.DataItem %></td>
</tr>
</ItemTemplate>
</asp:ListView>
Indeed, this the way to approach binding IEnumerable<T> where T is any value type – int, double, bool, etc. Container.DataItem returns the string representation of the value you are binding to. In the case of bool, it renders True and False.
Hope that helps. Happy coding!