views:

106

answers:

2

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.

-SK

+4  A: 

Don't use Eval. Bind it directly:

<%# Container.DataItem %>
onof
I already tried this..did not work :(
Sash
weird, it works in my code. Are you sure the strings in the list are not empty?
onof
A: 

Hi Sash,

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"

-D

Diego C.