将用户定义的对象列表绑定到转发器控件

时间:2014-07-05 12:53:08

标签: 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>();

我已用对象项填充此列表,因此它现在包含一些学生信息。谁能告诉我如何将它绑定到转发器控制器?

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

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 Page

<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>