嵌套Repeater实现页面导航

来源:互联网 发布:苏联艺术知乎 编辑:程序博客网 时间:2024/06/06 00:10

 

//前台代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   

    <div>

    <asp:Repeater id="rptCategories" runat="server"
            onitemdatabound="rptCategories_ItemDataBound">
  <HeaderTemplate>
    <table  border="0" cellspacing="0" cellpadding="0">
  </HeaderTemplate>
  <ItemTemplate>
    <!--分类名称-->
    <tr><th><%# DataBinder.Eval(Container.DataItem, "TypeName") %></th></tr>
    <!--分类下的产品-->
    <asp:Repeater id="rptProduct" runat="server">
      <ItemTemplate>
        <tr><td><a href='ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "ID") %>'><%# DataBinder.Eval(Container.DataItem, "ProduceName")%></a></td></tr>
      </ItemTemplate>
    </asp:Repeater>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>

</div>

    </div>
    </form>
</body>
</html>





//后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace WebApplicationUI
{
    public partial class Repeater嵌套Repeater实现页面导航 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bindRepeater();//绑定第一级父节点
        }

 


        //绑定主Repeater
        void bindRepeater()
        {
            BLL.ProductBll procduct=new BLL.ProductBll();
            rptCategories.DataSource = procduct.getProduct();
            rptCategories.DataBind();
       
         }

        //在绑定分类品名时,绑定分类下的产品
        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            BLL.ProductBll products = new BLL.ProductBll();
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptProduct = (Repeater)e.Item.FindControl("rptProduct");
                //找到分类Repeater关联的数据项
                DataRowView row = (DataRowView)e.Item.DataItem;
                //提取分类ID
                int CategorieId = Convert.ToInt32(row["ID"]);
                //根据分类ID查询该分类下的产品,并绑定产品Repeater
                rptProduct.DataSource = products.getProduceById(CategorieId);
                rptProduct.DataBind();
            }
        }
 
    }
}