为什么我得到错误
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>
答案 0 :(得分:2)
将公共变量name
转换为如下属性:
private string _name;
public string name
{
get
{
return _name??"Name";
}
set
{
_name = value;
}
}
它可以是自动属性,如果您不需要默认值("Name"
),如:
public string name {get;set;}