11

How do I bind a List to a in ASP.NET 3.5

  <asp:ListView ID="lvDiagnosisCodes" runat="server">
            <LayoutTemplate>
                <ul>
                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                </ul>
            </LayoutTemplate>
            <ItemTemplate>
                <li>
                    <%# Eval("Name") %>
                </li>
            </ItemTemplate>

        </asp:ListView>

I am not too sure what do use in the Eval part. Since this is a generic List of string , there is no column name.

Thanks in advance.

2 Answers 2

19

Don't use Eval. Bind it directly:

<%# Container.DataItem %>
Sign up to request clarification or add additional context in comments.

1 Comment

weird, it works in my code. Are you sure the strings in the list are not empty?
2

All you have to do is create an anonymous object and set it to the Datasource property of your Listview.

So if you have a list or an array of strings, do the following:

Dim myListOfStuff() As String = Manager.FetchMyStuff()

Me.lvDiagnosisCodes.DataSource = (From s In myListOfStuff Select New With {.Name = s}).ToArray
Me.lvDiagnosisCodes.DataSource.DataBind()

This way, you can use <%# Eval("Name") %> in the front end and bind to a "property"

Comments

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.