将ListView绑定到ArrayList和Display

时间:2014-04-07 20:16:35

标签: asp.net .net vb.net listview data-binding

我通常不会使用这些类型的控件,所以我可能在这里完全走错了路。

最终目标是拥有ProductTitle,ProductURL和ProductDescription的记录数据集。然后将这些记录以3列格式显示,如果需要还有其他行。例如:

记录1,2,3,4,5,6和7应显示

1 - 2 - 3

4 - 5 - 6

7

我得到的错误是

  

System.Web.HttpException:DataBinding:   ' System.Web.UI.WebControls.ListItem'不包含属性   名称' ProductTitle'。

前端方面:

<div>
    <asp:ListView ID="ListView1" runat="server" GroupItemCount="3" OnLoad="ListView1_Load">

        <LayoutTemplate>
            <table>
                <tr>
                    <td>
                        <table border="0" cellpadding="5">
                            <asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
                        </table>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>

        <GroupTemplate>
            <tr>
                <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
            </tr>
        </GroupTemplate>

        <ItemTemplate>
            <td>
                <div>
                    <div><%#Eval("ProductTitle")%></div>
                    <img alt="Test Image" src="<%#Eval("ProductURL")%>" />
                    <div><%# Eval("ProductDescription")%></div>
                </div>
            </td>
        </ItemTemplate>

    </asp:ListView>  
</div>

我已经获得了Code-Behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub


Protected Sub ListView1_Load(sender As Object, e As EventArgs)
    Dim myPaaams As ArrayList = New ArrayList

    myPaaams.Add(New ListItem("ProductTitle", "Adams Test"))
    myPaaams.Add(New ListItem("ProductURL", "Adams Test"))
    myPaaams.Add(New ListItem("ProductDescription", "Adams Test"))

    ListView1.DataSource = myPaaams
    ListView1.DataBind()
End Sub

我还尝试以下代码用于我的ListView加载事件,但是失败并出现同样的错误。

Protected Sub ListView1_Load(sender As Object, e As EventArgs)
    Dim myParams As ArrayList = New ArrayList

    Dim variable As New Dictionary(Of String, String) From {{"ProductTitle", "Adams Test"}, _
                                                            {"ProductURL", "value2"}, _
                                                            {"ProductDescription", "value2"}}

    myParams.Add(variable)

    ListView1.DataSource = myParams
    ListView1.DataBind()
End Sub

1 个答案:

答案 0 :(得分:1)

您需要使用类,例如

Public Class Products
    Public Property ProductTitle As String
    Public Property ProductURL as String
    Public Property ProductDescription as String
End Class


Protected Sub ListView1_Load(sender As Object, e As EventArgs)
    Dim myParams As BindingList(of Products)= New BindingList(of Products)

    Dim p as Products = New Products With {.ProductTitle = "Title", 
                                         .ProductURL = "URL",
                                         .ProductDescription="Description"}

    myParams.Add(p)
    ListView1.DataSource = myParams
    ListView1.DataBind()
End Sub

你正在尝试的东西不工作的原因是因为你试图手动创建一个新的列表项,然后绑定它 - 这将无法工作,因为列表项没有正确的财产。