Casting from Generic IList to Object during DataBind
Okay, so I have a Generic IList of custom objects that I want to use as a datasource for a repeater.
Dim productRows 开发者_StackOverflow中文版As IList(Of MyCustomObject)()
'fill list with data
rptResults.DataSource = productRows
rptResults.DataBind()
In the WebForm inside the repeater, I place this code:
<%# DirectCast(Container.DataItem, MyCustomObject).Title %>
And when I try to run it, I get the error "Unable to cast object of type 'System.Collections.Generic.List`1[MyCustomObject]' to type 'MyCustomObject'. ". Which makes sense, really.
What's the best way round this? Is there any way I can get the direct cast working? I'm loath to have to create a custom list item that implements IBinding or something and bind that directly, as that'll mean a lot of legacy changes which may create further bugs. Or is there a better way?
Try this
<%# Bind("Title") %>'
精彩评论