What Was I Thinking?

Follies & Foils of .NET Development
posts - 77, comments - 203, trackbacks - 0

My Links

News

Archives

Post Categories

Check These Out

Gurus

ASP.NET Data Binding from a Latebound IEnumerable<T>

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
  1. <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
  1. <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" DataSource='<%#DataBinder.Eval(Container.DataItem,"Products")%>'>

 

Happy Coding!

Print | posted on Friday, October 08, 2010 10:17 AM | Filed Under [ Visual Studio ]

Feedback

Gravatar

# re: ASP.NET Data Binding from a Latebound IEnumerable<T>

DataBinder is a .net framework class. You can learn more about it here.. http://msdn.microsoft.com/en-us/library/system.web.ui.databinder.aspx
10/9/2010 7:19 AM | Chris
Gravatar

# re: ASP.NET Data Binding from a Latebound IEnumerable<T>

Container.DataItem is an alias for the current item in the datasource. In other words, if you are binding to a collection of objects Container.DataItem is the current row of that collection.
11/9/2010 7:04 AM | wtfChris
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: