开发者

Casting from Generic IList to Object during DataBind

开发者 https://www.devze.com 2022-12-21 00:50 出处:网络
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)()

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") %>'
0

精彩评论

暂无评论...
验证码 换一张
取 消