DataBinding to an ArrayList

September 6, 2007

Maybe it’s because it’s late o’clock, but this one took me a while to figure out:

I have a DataList which is databound to an ArrayList containing objects of type MyType. I tried using something like this to print out the property MyProperty:

<asp:Label
runat=”server”
ID=”MyLabel”
Text=’<%# DataBinder.Eval(Container.DataItem, MyProperty) %>‘/>

Which of course failed. All I needed to do was to change it to this:

<asp:Label
runat=”server”
ID=”MyLabel”
Text=’<%# ((MyType)Container.DataItem).MyProperty %>‘/>

Note to self: don’t ever do it again!

Leave a Reply