将用户定义对象列表绑定到重复器控件
本文关键字:控件 绑定 用户 定义 对象 列表 | 更新日期: 2023-09-27 18:11:45
我有以下类
class stu
{
public string username;
public string password;
public string fname;
public string lname;
public string faculty;
}
假设我已经创建了这个列表:
list<stu>friends=new list<stu>();
我已经用对象项填充了这个列表,所以它现在包含了一些学生信息。有人能告诉我如何将它绑定到中继器控制器上吗?
我想这就是你要找的:
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();
}
。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>