c# - 如何将 List<string> 绑定(bind)到 WebForms 中的 ListView

标签 c# asp.net list listview

我有一个 List<string>我想绑定(bind)到我的 ListView 中的集合.

下面是我的 ListView 的标记:

<asp:ListView ID="lvList" runat="server">

        <LayoutTemplate>         
            <div id="Div1" runat="server">              
                <div ID="itemPlaceholder" runat="server">              
                </div>         
            </div>      
        </LayoutTemplate>

        <EmptyDataTemplate>         
            <div id="Div2" runat="server">              
                <div ID="itemPlaceholder" runat="server">                 
                No data was returned.             
                </div>         
            </div>      
        </EmptyDataTemplate>

        <ItemTemplate>
            <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval ("theList") %>'/>
        </ItemTemplate>

</asp:ListView>

在我的代码隐藏中:

protected void Page_Load(object sender, EventArgs e)
{         
    List<string> theList = new List<string>();
    //populate members of list
    lvList.DataSource = theList;
    lvList.DataBind();
}

错误信息:

System.Web.HttpException was unhandled by user code
Message="DataBinding: 'System.String' does not contain a property with the name 'theList'."

我觉得我在这里做错了,有人可以给我建议吗?

最佳答案

使用 '<%# Container.DataItem %> :

<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Container.DataItem %>'/>

关于c# - 如何将 List<string> 绑定(bind)到 WebForms 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185992/

相关文章:

c# - 缓存 Active Directory 数据

注销后 ASP.Net session 未失效

c# - Substring() 的问题 - ArgumentOutOfRangeException

python - 在Python中将项目插入排序列表

python-2.7 - 如何在 python 列表中追加行?

c# - 当使用自定义属性引入破坏代码时,我们可以强制出现编译时错误吗?

c# - 在商业应用中使用 Automapper 和 LinFu.DynamicProxy

c# - 自定义SynchronizationContext在unittest中异步设置Thread.CurrentPrincipal

asp.net - Facebook Connect 和 ASP.NET

python - 获取内部嵌套字典,其键存储在列表中