【发布时间】:2011-04-01 08:07:54
【问题描述】:
我在尝试将通用列表绑定到转发器时遇到了一些问题。泛型列表中使用的类型实际上是一个结构体。
我在下面构建了一个基本示例:
struct Fruit
{
public string FruitName;
public string Price; // string for simplicity.
}
protected void Page_Load(object sender, EventArgs e)
{
List<Fruit> FruitList = new List<Fruit>();
// create an apple and orange Fruit struct and add to List<Fruit>.
Fruit apple = new Fruit();
apple.FruitName = "Apple";
apple.Price = "3.99";
FruitList.Add(apple);
Fruit orange = new Fruit();
orange.FruitName = "Orange";
orange.Price = "5.99";
FruitList.Add(orange);
// now bind the List to the repeater:
repFruit.DataSource = FruitList;
repFruit.DataBind();
}
我有一个简单的结构来模拟 Fruit,我们有两个属性,即 FruitName 和 Price。我首先创建一个“FruitList”类型的空通用列表。
然后我使用结构创建两个水果(苹果和橙子)。然后将这些水果添加到列表中。
最后,我将通用列表绑定到转发器的 DataSource 属性...
标记如下所示:
<asp:repeater ID="repFruit" runat="server">
<ItemTemplate>
Name: <%# Eval("FruitName") %><br />
Price: <%# Eval("Price") %><br />
<hr />
</ItemTemplate>
我希望看到打印在屏幕上的水果名称和价格,由水平线分隔。
目前我收到与实际绑定有关的错误...
**Exception Details: System.Web.HttpException: DataBinding: '_Default+Fruit' does not contain a property with the name 'FruitName'.**
我什至不确定这是否可行?有什么想法吗?
谢谢
【问题讨论】:
-
随机说明,ListView 类在能力上已经大大取代了中继器。
-
@Chris Marisic 感谢您的提示,我现在正在阅读有关 ListView 的信息,看起来非常好:weblogs.asp.net/scottgu/archive/2007/08/10/…
标签: c# asp.net list repeater generics