Repeater 控件的嵌套使用
ItemDataBound:数据绑定的时候(正在进行时)发生,多用在Repeater控件嵌套,对子Repeater控件进行数据绑定及模板列中统计列的计算处理等事情
ItemCommand :用来响应Item模板中的控件的事件。
绑定数据时,在父Repeater的ItemDataBound事件中绑定子Repeater,在子Repeater的ItemDataBound事件中绑定孙Repeater:
<dl id="body-zhanshi">.aspx
<%--一级 Repeater 绑定 省 --%>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<dt class="body-dt">
<a href="#?AC=<%#Eval("AreaCode") %>">
<%#Eval("AreaName")%>
</a>
</dt>
<dd class="body-dd">
<%--二级 Repeater 绑定 市 --%>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<a href="#?AC=<%#Eval("AreaCode") %>"> <%#Eval("AreaName")%></a>
</ItemTemplate>
</asp:Repeater>
</dd>
</ItemTemplate>
</asp:Repeater>
</dl>
</div>
using System;View Code
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Thc_chengshi_Thc_chsh : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.ItemDataBound += Repeater1_ItemDataBound; //委托
//全部城市
Repeater1.DataSource = states("0001");
Repeater1.DataBind();
}
//母级城市对应的子集城市
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rep = e.Item.FindControl("Repeater2") as Repeater; //找到里层的repeater对象
ChinaStates rowv = (ChinaStates)e.Item.DataItem;//找到分类Repeater关联的数据项
string cod = rowv.AreaCode; //读取Repeater1中绑定数据的字段
rep.DataSource = states(cod); //调用方法查询数据、绑定Repeater1
rep.DataBind();
}
}
//查询城市的方法
public List<ChinaStates> states(string code)
{
List<ChinaStates> clist = new List<ChinaStates>();
using (Data0216_ltDataClassesDataContext con = new Data0216_ltDataClassesDataContext())
{
clist = con.ChinaStates.Where(r => r.ParentAreaCode == code).ToList();
}
return clist;
}
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
-- 指触发对象的类型是DadaList里的基本行或是替换行(简单的说是Repeater里的所有数据项内容)
Repeater rep = e.Item.FindControl("Repeater2") as Repeater;
-- 找到里层的repeater对象
ChinaStates rowv = (ChinaStates)e.Item.DataItem;
-- 找到 Repeater 关联的数据项
-- ChinaStates 是绑定数据的数据库表