经常会遇到需要嵌套的地方,比如说回复帖子等,这时候可以使用Repeater来进行嵌套,据说同样适用于DataList,代码如下:

ASPX代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="wwt.UI.WebForm1" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title>无标题页</title> 
    <style type="text/css"> 
    ul{  
    list-style: none;  
    }  
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <ul> 
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> 
        <ItemTemplate> 
        <li> 
            <%#Eval("Body")%> 
        </li>    
                <asp:Repeater ID="Repeater2" runat="server"> 
                <ItemTemplate> 
                <li style="text-indent:24px;"> 
                    <%#Eval("Body")%> 
                </li> 
                </ItemTemplate> 
                </asp:Repeater>                 
   
        </ItemTemplate> 
        </asp:Repeater> 
    </ul> 
    </div> 
    </form> 
</body> 
</html>

CS代码:

using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;  
using wwt.SQLServerDAL;  
 
namespace wwt.UI  
{  
    public partial class WebForm1 : System.Web.UI.Page  
    {  
        wwt.SQLServerDAL.Space SpaceDal = new wwt.SQLServerDAL.Space();  
        protected void Page_Load(object sender, EventArgs e)  
        {  
            //绑定外层的Repeater1  
            this.Repeater1.DataSource = SpaceDal.GetAlbumCommonListByID(101011, 0).Tables[0];  
            this.Repeater1.DataBind();  
        }  
 
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
        {  
            //判断里层repeater处于外层repeater的哪个位置( AlternatingItemTemplate,FooterTemplate,  
 
            //HeaderTemplate,,ItemTemplate,SeparatorTemplate)  
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)  
            {  
                Repeater rep = e.Item.FindControl("Repeater2") as Repeater;//找到里层的repeater对象  
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项   
                int typeid = Convert.ToInt32(rowv["id"]); //获取填充子类的id   
                rep.DataSource = SpaceDal.GetAlbumCommonListByID(101011, typeid).Tables[0];  
                rep.DataBind();  
            }  
        }  
    }  
}