页面代码
<form id="form1" runat="server"> <asp:Repeater ID="rptCategories" runat="server" OnItemCommand="rptCategories_ItemCommand"> <HeaderTemplate> <table width="100%" border="1" cellspacing="0" cellpadding="0"> </HeaderTemplate> <ItemTemplate> <!--分类名称--> <tr> <th> <asp:LinkButton ID="LinkButton1" CommandName=‘<%#DataBinder.Eval(Container.DataItem, "ID") %>‘ runat="server"><%#DataBinder.Eval(Container.DataItem, "ID")%></asp:LinkButton> <input type="button" value="收" onclick="aa()" /> </th> <th> <label runat="server" id="lbl"> <%# DataBinder.Eval(Container.DataItem, "loginid")%></label> </th> </tr> <!--分类下的产品--> <asp:Repeater ID="rptProduct" runat="server"> <ItemTemplate> <tr> <td> <%# DataBinder.Eval(Container.DataItem, "Course")%> </td> <td> <%# DataBinder.Eval(Container.DataItem, "Score")%> </td> </tr> </ItemTemplate> </asp:Repeater> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form>
根据点击Repeater上的按钮,在这行下方嵌套Repeater
protected void rptCategories_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rptProduct = (Repeater)e.Item.FindControl("rptProduct"); //找到分类Repeater关联的数据项 DataRowView rowv = (DataRowView)e.Item.DataItem; var a = e.Item.Controls[0]; //提取分类ID string id = e.CommandName; //string CategorieId = Convert.ToString(rowv["Name"]); //int CategorieId = Convert.ToInt32(rowv["ID"]); //根据分类ID查询该分类下的产品,并绑定产品Repeater List<Student> list = new List<Student>(); Student stu = new Student() { Course = "语文", Score = 135 }; list.Add(stu); list.Add(new Student() { Course = "数学", Score = 141 }); //绑定嵌套数据 rptProduct.DataSource = list; rptProduct.DataBind(); } }
绑定原数据
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (!IsPostBack) { List<Student> list = new List<Student>(); Student stu = new Student() { ID = 100, Name = "赵六", Sex = "男", Phone = "15889898998" }; list.Add(stu); list.Add(new Student() { ID = 102, Name = "王五", Sex = "男", Phone = "136*******8382" }); list.Add(new Student() { ID = 103, Name = "卡琳", Sex = "女", Phone = "138*******9083" }); list.Add(new Student() { ID = 105, Name = "王五", Sex = "男", Phone = "157*******7002" }); DataTable dt = new DataTable(); DataSet ds = GetDataSet("select * from profile"); rptCategories.DataSource = (DataTable)ds.Tables[0]; rptCategories.DataBind(); } } }
时间: 2024-10-17 06:04:34