我有一个List<string>
集合,我想将其绑定到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>
在我的CodeBehind中:
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未被用户代码
处理 Message =“DataBinding:'System.String'不包含属性 名称'theList'。“
我觉得我在这里做错了,有人可以告诉我吗?
答案 0 :(得分:9)
使用'<%# Container.DataItem %>
:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Container.DataItem %>'/>
答案 1 :(得分:1)
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval ("theList") %>
这一行导致了这个问题,因为你在列表的当前元素中引用了属性'theList',但是列表中除了里面的字符串之外没有任何属性。
可行的方法是例如在代码中实现方法,例如
protected void Test(object obj)
{
return obj.ToString();
}
并在aspx中:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Test(Container.DataItem) %>
我没有测试过,但它应该可以解决问题。
答案 2 :(得分:1)
EVAL用于单个键值,数据绑定控件从头到尾遍历集合,并逐个放置在放置eval语句的位置。
此链接中的答案可以提供更好的主意。 How to retrieve current object from collections using DataBinder.Eval?
希望这会有所帮助