在Repeater中嵌套使用Repeater
2024-07-21 02:16:01
供稿:网友
 
  在一般的网站中浏览类别的用户控件通常都位于大多数 asp.net 页的左边,它使用户能够按类别快速的查找产品。最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加进去。一来更方便,二来加长了左部导航栏的长度使页面更协调。原有的分类导航栏是由repeater实现的,现在需要在每一个分类下加入该类的商品信息,于是我想到了在原有repeater中嵌套repeater。实现界面如下: 
 
前台页面部分: 
<asp:repeater id="rptcategories" runat="server"> 
  <headertemplate> 
    <table width="100%" 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, "productname") %></a></td></tr> 
      </itemtemplate> 
    </asp:repeater> 
  </itemtemplate> 
  <footertemplate> 
    </table> 
  </footertemplate> 
</asp:repeater> 
 
后台代码部分(部分代码): 
//在绑定分类品名时,绑定分类下的产品 
private void rptcategories_itemdatabound(object sender, system.web.ui.webcontrols.repeateritemeventargs e) 
{ 
    bll.products products =new bll.products(); 
    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; 
        //提取分类id 
        int categorieid = convert.toint32(rowv["id"]); 
        //根据分类id查询该分类下的产品,并绑定产品repeater 
        rptproduct.datasource = products.getproductsbycategorieid(categorieid); 
        rptproduct.databind(); 
    } 
}