Many of the examples of databinding use datasource or objectsource controls. I prefer to databind from the code-behind. I feel I have finer control over how to data is prepared before I load it.
The downside to doing this “late binding” of the data, and I can’t use the strongly typed names in my declarative markup. The following code fails because the compiler can’t resolve the “Products” reference.
Code Snippet
- <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" DataSource='<%#Products%>'>
instead, you must use the Eval() method of the Databinder to resolve the references. The following works fine for any type of binding (late or early):
Code Snippet
- <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" DataSource='<%#DataBinder.Eval(Container.DataItem,"Products")%>'>
Happy Coding!