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.

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Sash
  • 3,525
  • 3
  • 19
  • 17

2 Answers2

19

Don't use Eval. Bind it directly:

<%# Container.DataItem %>
onof
  • 17,167
  • 7
  • 49
  • 85
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"

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Diego C.
  • 2,271
  • 2
  • 20
  • 21