今天看了dragonpig与dudu的改进版本的数据绑定,在此测试了一下
新建页面Student.aspx页面
public partial class Test_Student : System.Web.UI.Page
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
var students = new[] {
new Student{Name="mike",Age=23},
new Student{Name="jane", Age=12},
new Student{Name="frank",Age=25},
new Student{Name="susan",Age=32},
};
rptStudents.DataSource = students;
DataBind();
}
protected virtual TREsult ExpHelper<TEntity, TREsult>(Func<TEntity, TREsult> func)
{
var item = base.GetDataItem();
return func((TEntity)item);
}
protected TResult Eval<TResult>(Func<Student, TResult> func)
{
return ExpHelper<Student, TResult>(func);
}
}
在前台添加
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ul>
<asp:Repeater ID="rptStudents" runat="server">
<ItemTemplate>
<li>
<%#Eval(n=>n.Age)%>---<%#Eval(n=>n.Name)%>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</form>
</body>
</html>