绑定类型结构的一个泛型列表到中继器 [英] Binding a Generic List of type struct to a Repeater

查看:151
本文介绍了绑定类型结构的一个泛型列表到中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有点试图绑定一个泛型列表到中继器的问题。在通用列表中使用的类型实际上是一个结构。

I've had a bit of a problem trying to bind a generic list to a repeater. The type used in the generic list is actually a struct.

我已经建立了下面一个基本的例子:

I've built a basic example below:

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和价格。我通过创建类型的空泛型列表'FruitList启动。

I have a simple struct to model Fruit, we have two properties which are FruitName and Price. I start by creating an empty generic list of type 'FruitList'.

然后我创建一个使用结构(苹果和橘子)两种水果。这些水果然后加入到列表中。

I then create two fruits using the struct (apple and orange). These fruits are then added to the list.

最后,我绑定泛型列表的直放站的DataSource属性...

Finally, I bind the generic list to the DataSource property of the repeater...

的标记看起来是这样的:

The markup looks like this:

<asp:repeater ID="repFruit" runat="server">
<ItemTemplate>
    Name: <%# Eval("FruitName") %><br />
    Price: <%# Eval("Price") %><br />
    <hr />
</ItemTemplate>

我希望看到的水果名称和价格印在屏幕上,由一个水平线分隔。

I expect to see the fruit name and price printed on the screen, separated by a horizontal rule.

目前我正在与实际绑定错误...

At the moment I am getting an error relating to actual binding...

**Exception Details: System.Web.HttpException: DataBinding: '_Default+Fruit' does not contain a property with the name 'FruitName'.**

我甚至不知道这是否可以工作?任何想法?

I'm not even sure if this can work? Any Ideas?

感谢

推荐答案

您需要将您的公共领域变成一个公共属性。

You need to change your public field into a public property.

更改此:公共字符串FruitName;

要:

public string FruitName { get; set; }

否则你可以做fruitName私人和包括公共财产吧。

Otherwise you could make fruitName private and include a public property for it.

private string fruitName;

public string FruitName { get { return fruitName; } set { fruitName = value; } }

这里是别人的链接谁拥有了同样的问题,因为你。

这篇关于绑定类型结构的一个泛型列表到中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆