2016-09-15 42 views
0

C#绑定泛型列表在Asp.Net

List<Rating> ratingList = new List<Rating>(); 

public class Rating 
{ 
    public string CustomerName; 
} 

RptCustomerRating.DataSource = ratingList; 
RptCustomerRating.DataBind(); 

在debugmode列表被填满。但在ItemTemplate评估和演示不工作在所有...

aspx文件版本1

<asp:Repeater ID="RptCustomerRating" runat="server"> 
     <ItemTemplate> 
      <div class="row"> 
        <div class="col-md-12"> 
         <h2><%#Eval("CustomerName") %></h2> 
        </div> 
      </div> 
     </ItemTemplate> 
    </asp:Repeater> 

我的错误:
System.Web.HttpException:数据绑定:装箱+评级不包含财产命名为CustomerName。

aspx文件第2版

<asp:Repeater ID="RptCustomerRating" runat="server"> 
     <ItemTemplate> 
      <asp:Repeater ID="inner" runat="server"> 
       <ItemTemplate> 
        <div class="row"> 
         <div class="col-md-12"> 
          <h2><%#Eval("CustomerName") %></h2> 
         </div> 
        </div> 
       </ItemTemplate> 
      </asp:Repeater> 
     </ItemTemplate> 
    </asp:Repeater> 

这是我在计算器中。有人建议用嵌套中继器来解决这个问题。那么Visual Studio不能找到一个错误,但在这种情况下并没有发生。 html部分是空的。

回答

2

CustomerName

public class Rating 
{ 
    public string CustomerName {get; set;} 
} 
+0

感谢的是解决我的问题:) – JulZc

0

您使用的是现场,没有房产需要getset财产。这就是为什么你有这样的错误。

public class Rating 
{ 
    public string CustomerName; // <= this is a field 
} 

public class Rating 
{ 
    public string CustomerName { get; set;} // <= this is a property 
} 

Difference between fields and properties