【问题标题】:Bind ListView to ArrayList and Display将 ListView 绑定到 ArrayList 并显示
【发布时间】:2014-05-20 06:34:08
【问题描述】:

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

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

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

1 - 2 - 3

4 - 5 - 6

7

我得到的错误是

System.Web.HttpException:数据绑定: '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>

我有我的代码隐藏:

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 Load 事件尝试过以下代码,但失败并出现同样的错误。

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

【问题讨论】:

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


    【解决方案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
    

    您尝试的方法不起作用的原因是您尝试手动创建一个新的列表项,然后对其进行绑定——这不起作用,因为列表项没有正确的属性。

    【讨论】:

    • 刚刚尝试过,但我仍然收到相同的错误消息。错误发生在 ListView1.DataBind()
    • 我修正了我的代码中的拼写错误,将您的 aspx 代码复制到一个新项目中(编辑以将 img 更改为 div)并将我的代码粘贴到代码隐藏中 - 工作(在为绑定列表添加导入之后)。
    • 这是一个 ASCX 而不是一个 ASPX 有区别吗?我们在 DNN 中工作,所有代码都在单独的模块/控件中完成,而不是整页。
    • 没关系,我复制了你修改后的代码,它工作得很好。我以为我自己已经修正了错别字,但显然没有。谢谢你的帮助!!!
    • 不客气。我要补充一点,ArrayList 是 .Net 1.0 的遗物,我不建议将它用于任何新事物。在这种特殊情况下(在另一边使用 eval )可能没有太大区别,但仍然......为错别字道歉,iPad 很棒,但确实有一些限制。
    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多