C# 将用户定义对象列表绑定到repeater控件
我有以下课程C# 将用户定义对象列表绑定到repeater控件,c#,asp.net,repeater,C#,Asp.net,Repeater,我有以下课程 class stu { public string username; public string password; public string fname; public string lname; public string faculty; } 假设我创建了以下列表: list<stu>friends=new list<stu>(); listfriends=newlist(); 我已经用对象项填充了这个
class stu
{
public string username;
public string password;
public string fname;
public string lname;
public string faculty;
}
假设我创建了以下列表:
list<stu>friends=new list<stu>();
listfriends=newlist();
我已经用对象项填充了这个列表,所以它现在包含了一些学生信息。有谁能告诉我如何将其绑定到中继器控制器上吗?我想这就是您要寻找的:
class stu
{
public string username;
public string password;
public string fname;
public string lname;
public string faculty;
}
protected void Page_Load(object sender, EventArgs e)
{
List<stu>friends = new List<stu>();
foreach (var friend in friends)
{
friends.Add(
new Friends { Fname = friend.fname, LName = friend.lname, Faculty = friend.faculty }
);
}
this.rptFriends.DataSource = friendsList;
this.rptFriends.DataBind();
}
stu类
{
公共字符串用户名;
公共字符串密码;
公共字符串fname;
公共字符串名称;
公共弦乐学院;
}
受保护的无效页面加载(对象发送方、事件参数e)
{
Listfriends=新列表();
foreach(varfriends in friends)
{
朋友,加上(
新朋友{Fname=friend.Fname,LName=friend.LName,Faculty=friend.Faculty}
);
}
this.rptFriends.DataSource=friendsList;
this.rptFriends.DataBind();
}
.aspx页
<asp:Repeater ID="rptFriends" runat="server">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Fname</th>
<th>LName</th>
<th>Faculty</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Fname") %></td>
<td><%# Eval("lName") %></td>
<td><%# Eval("Faculty") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
Fname
名字
官能
但我需要数据源作为列表,因此中继器会逐个对象获取数据源。我看不到您在代码中将列表设置为数据源。它不起作用,请检查一下好吗?我得到了这个:不包含名为'fname'的属性问题是,您已经使用{get;set;}将类的属性设置为属性。这将起作用,谢谢,伙计,我忘了更改该部分,但是很好的捕获。干杯