前台代码:
<div> <asp:Repeater ID="rptOne" runat="server" OnItemDataBound="rptOne_ItemDataBound" > <ItemTemplate> <div > <%# Eval("DepartName") %> <div> <asp:Repeater ID="rptTwo" runat="server"> <ItemTemplate> <%# Eval("StaffName") %> </ItemTemplate> </asp:Repeater> </div> </div> </ItemTemplate> </asp:Repeater> </div>
后台代码:
public partial class WebForm1 : System.Web.UI.Page { StaffInfoBLL sbll = new StaffInfoBLL(); DepartmentInfoBLL dbll = new DepartmentInfoBLL(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.rptOne.DataSource = dbll.GetAll(); Session["DepartAll"] = sbll.GetAll(); this.rptOne.DataBind(); } } protected void rptOne_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rptWo = e.Item.FindControl("rptTwo") as Repeater; DepartmentInfo d = e.Item.DataItem as DepartmentInfo; IList<StaffInfo> list = Session["DepartAll"] as IList<StaffInfo>; if (list != null) { rptWo.DataSource = list.Where(p=>p.DepartmentInfo.DepartId==d.DepartId); rptWo.DataBind(); } } } }