2010-08-19 5 views
7

제네릭 목록을 리피터에 바인딩하는 데 문제가 있습니다. generic 목록에 사용 된 유형은 실제로 struct입니다.구조체 유형 일반 목록을 반복기에 바인딩

나는 기본적인 예를 아래에 내장했습니다

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'유형의 빈 제네릭 목록을 작성합니다.

그런 다음 struct (apple and orange)를 사용하여 두 개의 과일을 만듭니다. 이 과일은 목록에 추가됩니다.

마지막으로, 리피터의 데이터 소스 속성에 일반 목록 ...

마크 업이 다음과 같습니다 바인딩 :

<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'.** 

나는이 일을 할 수 있는지 모르겠어요? 어떤 아이디어?

감사합니다.

+1

랜덤 노트,리스트 뷰 클래스는 대규모 기능의 측면에서 중계기를 대체한다. –

+0

@Chris Marisic 팁을 주셔서 감사합니다. 지금 ListView에 대해 읽고 있습니다. 정말 좋아 보입니다. http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control- part-1-building-a-product-listing-page-with-clean-css-ui.aspx – Dal

답변

9

공개 입력란을 공개 속성으로 변경해야합니다.

변경이 : public string FruitName;

하려면 :

public string FruitName { get; set; } 

그렇지 않으면 당신은 fruitName 비공개과 이용을위한 공공 재산을 포함 할 수있다.

private string fruitName; 

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

Here is a link with someone who has had the same issue as you.

+0

그것은 성공했습니다! 맙소사, 지금 바보 같아, 이해가 돼, 정말 고마워 !! :) – Dal

1

오류는 필요한 모든 것을 알려줍니다. FruitName 및 Price에 대해 정의 된 속성이 아닌 public 필드가 있습니다.