I am working on a Web Forms project that loads Sql query results into DataTable s.
These DataTable are passed in front of the interface, where we bind them to Repeater web control.
This works great. However, now we want to bind to our own custom classes instead of a DataTable
. Unfortunately, what I thought was the obvious answer did not work (implementing IDictionary<string, object>
in our class).
What do we need to bind Eval to Datum without creating a specific property for each binding? Obviously, DataRow does not have to specifically implement every property that we bind. So, somehow it seems that Eval can look up property values by name in a DataRow.
Here is a custom class
public class Datum: IDictionary<string, object> { private Dictionary<string, object> _entries; public Datum() { _entries = new Dictionary<string, object>(); } public object this[string s] { get { return this._entries[s]; } } ... }
This is where the DataSource is installed in the aspx.cs file
rptTags.DataSource = new[] { new Datum { {"Count", 1} }, new Datum { {"Count", 2 } };
And here is the binding in aspx file
<asp:Repeater ID="rptTags" runat="server"> <ItemTemplate> <%# (int)Eval("Count") > </ItemTemplate> </asp:Repeater>
Using the above example, we get an error message saying that this property does not exist, which is true, but it also does not exist in the DataRow. How to do it like System.Data.DataRow?