預覽效果圖:
數據庫結構:
id(int) classname(string) parentid(int) sort(int用於顯示與排序)
1 家居 0 1
2 家電 0 2
3 沙發 1 1
4 某... 3 1
...
10 ...紅色 4 1
注:
parentid 父節點ID
sort 用於隱藏或顯示 兼排序功能
前台:
<asp:Repeater ID="rep" runat="server" onitemdatabound="rep_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate></ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:Repeater>
後台:
代碼
public partial class 遞歸2 : System.Web.UI.Page
{
public List<cmodel> list;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rules r = new rules();
list = r.getlist();//讀取數據庫
this.rep.DataSource = list.Where(x => x.parentid == 0).ToList();//LINQ2OBJECT
this.rep.DataBind();
}
}
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Literal l = new Literal();
l.ID = "ul";