首页 > 代码库 > Repeater嵌套

Repeater嵌套

效果图

HTML页面

 <asp:Repeater runat="server" ID="rptypelist" OnItemDataBound="rptypelist_ItemDataBound">      <ItemTemplate>          <div class="subnav_cp02">              <span><%#Eval("ItemName") %></span>                 <dl>                     <dd>                        <asp:Repeater runat="server" ID="rpquestionlist">                           <ItemTemplate>                              <a href="/ProductList.aspx?ID=<%#Eval("Id")%>"><%#Eval("ItemName") %></a>                           </ItemTemplate>                         </asp:Repeater>                       </dd>                   </dl>            </div>      </ItemTemplate>  </asp:Repeater>

引用空间

using System.Web.UI.WebControls;using System.Data;

.cs文件

  public void RpTypeBind()  {      
   //数据源绑定的是一张表
this.rptypelist.DataSource = Letao.BLL.ProductCategory.GetDataSet(0).Tables[0]; this.rptypelist.DataBind(); } //在绑定分类品名时,绑定分类下的产品 public void rptypelist_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rptProduct = (Repeater)e.Item.FindControl("rpquestionlist"); //找到分类Repeater关联的数据项 DataRowView rowv = (DataRowView)e.Item.DataItem; //提取分类ID int CategorieId = Convert.ToInt32(rowv["ID"]); //根据分类ID查询该分类下的产品,并绑定产品Repeater rptProduct.DataSource =Letao.BLL.ProductCategory.GetDataSet(CategorieId).Tables[0]; rptProduct.DataBind(); } }

备注:绑定大类时返回的是表结构

 

 

SQLSERVER类库

 /// <summary>        /// 根据父ID取得类别列表        /// </summary>        /// <param name="pItemId">父ID</param>        /// <returns></returns>        public DataSet GetDataSet(int ParentId)        {            string sql = "select * from ProductCategory where ParentId=" + ParentId + " and ItemName<>‘‘ order by SortValue asc,id asc";            return DBUtility.DbHelperSQL.ExecuteDataSet(ConnString.connReadonly, CommandType.Text, sql, null);        }
根据父ID取得类别列表

数据库结构

Repeater嵌套