我在尝试将一个泛型列表绑定到重复器时遇到了一些问题。泛型列表中使用的类型实际上是一个结构体。
下面是一个基本示例:
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();
}
我有一个简单的结构来模拟水果,我们有两个属性,分别是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'.**
我甚至不确定这是否可行?有什么想法吗?
谢谢。