数据绑定中继器。找不到物业

时间:2012-04-18 10:14:24

标签: asp.net repeater datasource

为什么我得到错误 DataBinding:_Default+Student does not contain a property called name.

这是我的CodeBehind:

public class Student
{
       public string name ="Name";
       public string favouriteFood = "Favourite food";
       public string hobby = "Hobby";

}

protected void Page_Load(object sender, EventArgs e)
{

     Student nino = new Student();
     nino.name = "nino";
     nino.favouriteFood = "nachos";
     nino.hobby = "dancing son";


     Student madelene = new Student();
     madelene.name = "madelene";
     madelene.favouriteFood = "sushi";
     madelene.hobby = "dancing casino";

     Student baiba = new Student();
     baiba.name = "baiba";
     baiba.favouriteFood = "bitch soup";
     baiba.hobby = "complaining";

     ArrayList students = new ArrayList();

     students.Add(madelene);
     students.Add(nino); 
     students.Add(baiba); 

     testRepeater.DataSource = students;
     testRepeater.DataBind();


}

这是前线:

 <asp:Repeater runat="server" ID="testRepeater" >
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' />"></asp:Label>
    </ItemTemplate>

</asp:Repeater>

1 个答案:

答案 0 :(得分:2)

将公共变量name转换为如下属性:

private string _name;
public string name
{
   get
   {
      return _name??"Name";
   }
   set
   {
      _name = value;
   }
}

它可以是自动属性,如果您不需要默认值("Name"),如:

public string name {get;set;}