If you have a ListView whose DataSource is set to a collection of typed objects, then the Eval() method will use reflection to access the object properties for each processed record. (This does not only apply to ListViews, but to any container capable of handling data bindings in templates)
Since we know what type of data will be displayed in the ListView, we do not need reflection, but can directly address the object underlying the current data item:
protected Foo CurrentItem(object Container) { return (Container as ListViewDataItem).DataItem as Foo; }
This allows the usual Eval() notation as in
<asp:Label runat="server" Text='<%# Eval("Bar") %>'>
to be replaced by
<asp:Label runat="server" Text='<%# CurrentItem(Container).Bar %>'>
While this works in principle, what is still missing is that the compiler throws an error if the databinding expression is invalid. This is due to the fact that ASP.Net compiles databindings only when the page is requested.
To force the compiler, instead of the runtime, to compile databindings, we need to add aspnet_compiler as a post-build event (thank you StackOverflow):
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler.exe -v / -p "$(ProjectDir)\"
and set the “Run the post-build event” option to “On successful build”.
(Note: .Net 3.5 also uses the 2.0 compiler, so no need to change the path)