Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a List<string> to a Listview in asp.net 3.5

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.

like image 470
Sash Avatar asked Jul 12 '10 19:07

Sash


2 Answers

Don't use Eval. Bind it directly:

<%# Container.DataItem %>
like image 141
onof Avatar answered Nov 09 '22 09:11

onof


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"

like image 23
Diego C. Avatar answered Nov 09 '22 07:11

Diego C.