李晓亮的博客

导航

【转】repeater多层嵌套

转自:http://hi.baidu.com/tgszsh/blog/item/fbc176297b9f14f698250a35.html

repeater三层嵌套案例,同样适用于多层

aspx文件:

<asp:Repeater ID="BigCategories" runat="server" OnItemDataBound="BigCategories_ItemDataBound">
        <ItemTemplate>
        <a href="/Product/Pro_TypeList_<%# DataBinder.Eval(Container.DataItem, "Cid") %>.htm"><h6 style="cursor:pointer"><%# DataBinder.Eval(Container.DataItem, "name") %></h6></a>
        <ul>
            <asp:Repeater ID="SmallCategories" runat="server" OnItemDataBound="SmallCategories_ItemDataBound">
             <ItemTemplate>
                <li class="l1"><a href="/Product/Pro_TypeList_<%# DataBinder.Eval(Container.DataItem, "Cid") %>.htm"><%# DataBinder.Eval(Container.DataItem, "name")%></a></li>
                    <asp:Repeater ID="proCategories" runat="server" >
                         <ItemTemplate>
                            <li class="l2"><a href="/Product/Pro_Details_<%# DataBinder.Eval(Container.DataItem, "articleid") %>.htm"><%# DataBinder.Eval(Container.DataItem, "title") %></a></li>
                         </ItemTemplate>
                 </asp:Repeater>
             </ItemTemplate>
             </asp:Repeater>           
        </ul>
        </ItemTemplate>
        </asp:Repeater>

 

cs文件:

protected void Page_Load(object sender, EventArgs e)
        {
            BindProductType();
        }

        protected void BindProductType()
        {
            //绑定产品大类
            CB_Article.BLL.CB_Proclass bll = new CB_Article.BLL.CB_Proclass();
            DataSet ds = bll.GetList(" parentid=0");
            if (ds.Tables[0].Rows.Count > 0)
            {
                BigCategories.DataSource = ds;
                BigCategories.DataBind();
            }
        }

        protected void BigCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            CB_Article.BLL.CB_Proclass sbll = new CB_Article.BLL.CB_Proclass();
            string sql = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptProduct = (Repeater)e.Item.FindControl("SmallCategories");
                rptProduct.ItemDataBound += new RepeaterItemEventHandler(SmallCategories_ItemDataBound);

                //这一句很重要

                //找到分类Repeater关联的数据项
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                //提取分类ID
                int CategorieId = Convert.ToInt32(rowv["CID"]);
                //根据分类ID查询该分类下的产品,并绑定产品Repeater
                sql = " parentid=" + CategorieId.ToString() + "";
                DataSet dst = sbll.GetList(sql);
                rptProduct.DataSource = dst;
                rptProduct.DataBind();
            }
        }

        protected void SmallCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            CB_Article.BLL.CB_Product sbll = new CB_Article.BLL.CB_Product();
            string sql = "";
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rptProduct = (Repeater)e.Item.FindControl("proCategories");              

                //找到分类Repeater关联的数据项
                DataRowView rowv = (DataRowView)e.Item.DataItem;
                //提取分类ID
                int CategorieId = Convert.ToInt32(rowv["CID"]);
                //根据分类ID查询该分类下的产品,并绑定产品Repeater
                sql = " cid=" + CategorieId.ToString() + "";
                DataSet dst = sbll.GetList("articleid,left(title,12) as title",sql);
                rptProduct.DataSource = dst;
                rptProduct.DataBind();
            }
        }

posted on 2009-06-30 00:52  LeeXiaoLiang  阅读(226)  评论(0)    收藏  举报