OnItemCreated方式
html
<%-- 一级Repeater绑定班级信息 --%>
<asp:Repeater ID="rptClassInfo" runat="server" OnItemCreated="rptClassInfo_ItemCreated">
<ItemTemplate>
<div class="classInfo">
<h3><%# Eval("ClassName") %></h3>
<div class="stuInfo">
<ul class="title">
<li>姓名</li>
<li>性别</li>
<li>年龄</li>
</ul>
<%-- 二级Repeater绑定学生信息 --%>
<asp:Repeater ID="rptStudentInfo" runat="server">
<ItemTemplate>
<ul class="<%# Container.ItemIndex%2==0?"cor1":"cor2" %>">
<%-- 根据单双行增加样式 --%>
<li><%# DataBinder.Eval(Container.DataItem,"Name") %></li>
<li><%# DataBinder.Eval(Container.DataItem,"Sex") %></li>
<li><%# DataBinder.Eval(Container.DataItem,"Age") %></li>
</ul>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
后端代码
cs
protected void Page_Load(object sender, EventArgs e)
{
BindClassInfo();
}
/// <summary>
/// 绑定学生信息(二级Repeater)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rptClassInfo_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptStudentInfo = e.Item.FindControl("rptStudentInfo") as Repeater;
if (rptStudentInfo != null)
{
ClassInfo classInfo = e.Item.DataItem as ClassInfo;
//如果是DataTable则DataItem是DataRowView
/*
DataTable dt = new DataTable();
dt.Columns.Add("id");
for (int i = 0; i < 10; i++)
{
var row = dt.NewRow();
row["id"] = i;
dt.Rows.Add(row);
}
var dataItem = e.Item.DataItem;
var row = dataItem as DataRowView;
Console.WriteLine(row["id"].ToString());
*/
if (classInfo != null && classInfo.StudentList != null)
{
rptStudentInfo.DataSource = classInfo.StudentList;
rptStudentInfo.DataBind();
}
}
}
}
/// <summary>
/// 绑定班级信息(一级Repeater)
/// </summary>
public void BindClassInfo()
{
//获取所有班级信息
List<ClassInfo> classInfoList = ClassInfo.GetClassInfoList();
rptClassInfo.DataSource = classInfoList;
rptClassInfo.DataBind();
}
直接嵌套
html
<%-- 一级Repeater绑定班级信息 --%>
<asp:Repeater ID="rptClassInfo" runat="server">
<ItemTemplate>
<div class="classInfo">
<h3><%# Eval("ClassName") %></h3>
<div class="stuInfo">
<ul class="title">
<li>姓名</li>
<li>性别</li>
<li>年龄</li>
</ul>
<%-- 二级Repeater绑定学生信息 --%>
<asp:Repeater ID="rptStudentInfo" runat="server" DataSource='<%#Eval("StudentList") %>'>
<ItemTemplate>
<ul class="<%# Container.ItemIndex%2==0?"cor1":"cor2" %>">
<%-- 根据单双行增加样式 --%>
<li><%# DataBinder.Eval(Container.DataItem,"Name") %></li>
<li><%# DataBinder.Eval(Container.DataItem,"Sex") %></li>
<li><%# DataBinder.Eval(Container.DataItem,"Age") %></li>
</ul>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
后端代码
cs
protected void Page_Load(object sender, EventArgs e)
{
BindClassInfo();
}
/// <summary>
/// 绑定班级信息(一级Repeater)
/// </summary>
public void BindClassInfo()
{
//获取所有班级信息
List<ClassInfo> classInfoList = ClassInfo.GetClassInfoList();
rptClassInfo.DataSource = classInfoList;
rptClassInfo.DataBind();
}
最终效果
方式1适合再次查找数据,比如再次查询数据库,方式2适合一次性查询出来直接绑定
字典和数组绑定
html
<asp:Repeater ID="dicBindRpt" runat="server">
<ItemTemplate>
<p>
<span><%#Eval("key") %></span>
<span><%#Eval("value") %></span>
</p>
</ItemTemplate>
</asp:Repeater>
<hr />
<asp:Repeater ID="arrayBindRpt" runat="server">
<ItemTemplate>
<p>
<%#Container.DataItem %>
</p>
</ItemTemplate>
</asp:Repeater>
后端代码
cs
var dic = new Dictionary<string, string>();
dic.Add("Name", "Nick");
dic.Add("Age", "13");
this.dicBindRpt.DataSource = dic;
this.dicBindRpt.DataBind();
var array = new string[]
{
"张三",
"李四",
"王五"
};
this.arrayBindRpt.DataSource = array;
this.arrayBindRpt.DataBind();