What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

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 8, 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
Gravatar

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

I want to know that how can I retrieve the row count of datalist which is nested inside gridview. Datalist is bind as follow:

<asp:DataList ID="dlbroker" RepeatColumns="3" CssClass="width_100" OnItemDataBound="dlbroker_OnItemDataBound" RepeatDirection="Horizontal" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "broker") %>'>

Now, I want to know the row count to use in
<FooterTemplate> </FooterTemplate>
11/21/2013 9:14 AM | Jaydeep Suryawanshi
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: