【原创】无限分级Repeater递归实现:读取一次数据库,使用LINQ2SQL技术,支持排序&显示隐藏...

预览效果图:

 

数据库结构:

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 >

 

 

后台:

 

ExpandedBlockStart.gif 代码
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 " ;
            l.Text
= " <ul> " ;
            e.Item.Controls.Add(l);
        }
        
else   if  (e.Item.ItemType  ==  ListItemType.Footer)
        {
            Literal l 
=   new  Literal();
            l.ID 
=   " ul2 " ;
            l.Text 
=   " </ul> " ;
            e.Item.Controls.Add(l);
        }
        
else   if  (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
        {
            
int  id  =  ( int )DataBinder.Eval(e.Item.DataItem,  " id " );
            Literal li1 
=   new  Literal();
            li1.ID 
=   " li1 " ;
            li1.Text 
=   " <li> " ;
            e.Item.Controls.Add(li1);

            Literal name 
=   new  Literal();
            name.ID 
=   " n " ;
            name.Text 
=  DataBinder.Eval(e.Item.DataItem,  " classname " ).ToString();
            e.Item.Controls.Add(name);
            List
< cmodel >  temp  =  list.Where(x  =>  x.parentid  ==  id).ToList();//LINQ2OBJECT
            
if  (temp.Count  >   0 )
            {
                Repeater r 
=   new  Repeater();
                TemplateBuilder tb 
=   new  TemplateBuilder();
                tb.AppendLiteralString(
"" );
                r.HeaderTemplate 
=  tb;
                r.FooterTemplate 
=  tb;
                r.ItemTemplate 
=  tb;
                r.ItemDataBound 
+=   new  RepeaterItemEventHandler(rep_ItemDataBound); //递归核心
                r.DataSource 
=  temp;
                r.DataBind();
                e.Item.Controls.Add(r);
            }
            Literal li2 
=   new  Literal();
            li2.ID 
=   " li2 " ;
            li2.Text 
=   " </li> " ;
            e.Item.Controls.Add(li2);
        }
    }

}

 

 

使用到的类:

ExpandedBlockStart.gif 代码
public   class  rules
{
    DataClasses1DataContext dc 
=   new  DataClasses1DataContext();

    
public  List < cmodel >  getlist()
    {
        var q 
=  from x  in  dc.ClassFJ
                
where  x.sort  >   0    // 用于控制显示隐藏
                orderby x.sort     // 兼职排序功能
                select  new  cmodel
                (
                    x.id,
                    x.className,
                    x.parentid
                );
        
return  q.ToList < cmodel > ();
    }
}

public   class  cmodel
{
    
public  cmodel( int  i, string  n, int  p)
    {
        id 
=  i;
        classname 
=  n;
        parentid 
=  p;
    }
    
public   int  id {  get set ; }
    
public   string  classname {  get set ; }
    
public   int  parentid {  get set ; }
}

 

 

 

抛砖引玉,话不多讲。

 

作者:Ryan

来自:http://flysnow-z.cnblogs.com/

 

转载于:https://www.cnblogs.com/flysnow-z/archive/2010/11/09/1872424.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值