0

I don't usually work with these types of controls, so I could be completely on the wrong track here..

The end goal is to have a dataset of records with ProductTitle, ProductURL and ProductDescription. Then have those records displayed in a 3 column format, with additional rows if needed. For example:

records 1, 2, 3, 4, 5 ,6 and 7 should show

1 - 2 - 3

4 - 5 - 6

7

The error I'm getting is

System.Web.HttpException: DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'ProductTitle'.

The Front-End side:

<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>

I've got my 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

I've also tried the following code for my ListView Load event, but this fails with the same error.

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 Answer 1

1

You need to use a class, such as

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

The reason why what you are trying isn't working, is because you are trying to manually create a new list item, and then bind against it -- that won't work, as the list item doesn't have the right properties.

Sign up to request clarification or add additional context in comments.

5 Comments

Just tried this, but I still get the same error message. The error occurs on ListView1.DataBind()
I fixed the typos in my code, copied your aspx code into a new project (editted to change img to div) and pasted my code into the codebehind--works (after adding import for bindinglist).
Does it make a difference that this is an ASCX and not an ASPX? We work in DNN and all code is done in separate modules / controls, instead of full pages.
Nevermind, I copied your revised code in and it works just fine. I thought I had fixed the typos myself but apparently not. Thank you for the help!!!
You're welcome. I'd add that ArrayList is a relic of .Net 1.0, and I wouldn't recommend using it for anything new. It may not make much of a difference in this particular scenario (using eval on the otherside), but still...Apologizes for the typos, iPads are great, but do have some limitations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.