动态创建Repeater,绑定数据

有一个菜单树,因为界面上的限制(要求)不能使用现有的控件。

那我就想到了用Repeater控件自己来写,可以灵活、方便的控制页面。

 

< asp:Repeater  ID ="CategoryList"  runat ="server"  OnItemDataBound ="CategoryList_ItemDataBound" >
    
< HeaderTemplate >
        
< div  style ="width: 90%; text-align:left; padding-left:10px;" >
    
</ HeaderTemplate >
    
< ItemTemplate >
        
< div  style ="line-height:30px;" >
            
< img  src ="images/jt_red.gif"  width ="3"  height ="5"   />< img  src ="images/jt_red.gif"
                width
="3"  height ="5"   />
            
< asp:HyperLink  ID ="hlRootCategory"  CssClass ="dh"  runat ="server" > Category </ asp:HyperLink >
        
</ div >
        
    
</ ItemTemplate >
    
< FooterTemplate >
        
</ div >
    
</ FooterTemplate >
</ asp:Repeater >
这里没有使用数据源控件。而是在后台动态绑定的数据。
         protected   void  Page_Load( object  sender, EventArgs e)
        {
            DataSet ds 
=   new  Aricc.ooxx.BLL.Category().GetList( " parentid=0 " );
            CategoryList.DataSource 
=  ds;
            CategoryList.DataBind();
        }

        
protected   void  CategoryList_ItemDataBound( object  sender, RepeaterItemEventArgs e)
        {
            DataRowView drv 
=  (DataRowView)e.Item.DataItem;
            HyperLink link 
=  (HyperLink)e.Item.FindControl( " hlRootCategory " );
            
if  (link  !=   null )
            {
                link.Text 
=  drv[ " categoryname " ].ToString();
                link.NavigateUrl 
=   " ../search.aspx?c= "   +  drv[ " id " ].ToString();

                ListInnerCategory(e.Item, 
int .Parse(drv[ " id " ].ToString()));
            }
        }
看到那个ListInnerCategory方法了吗?它就是递归显示子类数据方法。在这个方法里会动态的创建Repeater控件,并绑定相应的数据。
要动态创建Repeater控件,必须要定义Template。用于对Repeater的ItemTemplate进行赋值。而这个Template需要是实现了ITemplate接口的一个类的实例。
那么下面我们首先定义这样的一个模板类
     public   class  CategoryTemplate : ITemplate
    {

        
#region  ITemplate 成员
        
private   int  currentLevel;

        
public  CategoryTemplate( int  level)
        {
            currentLevel 
=  level;
        }
 
        
public   void  InstantiateIn(Control container)
        {

            HyperLink link 
=   new  HyperLink();
            link.ID 
=   " innerLink " ;

            HtmlGenericControl div 
=   new  HtmlGenericControl();
            div.TagName 
=   " div " ;
            div.Attributes.Add(
" style " " line-height:30px;text-indent: "   +  (currentLevel  *   20 +   " px; " );
            div.ID 
=   " innerDiv " ;
            div.Controls.Add(link);

            container.Controls.Add(div);
        }

        
#endregion
    }
接下来是ListInnerCategory的定义及相应的事件处理程序
         private   int  currentLevel  =   0 ;
        
private   void  ListInnerCategory(RepeaterItem item,  int  ID)
        {
            
if  ( new  Aricc.ooxx.BLL.Category().HasChild(ID))
            {
                currentLevel
++ ;
                DataSet ds 
=   new  Aricc.ooxx.BLL.Category().GetList( " parentid= "   +  ID);
                Repeater rep 
=   new  Repeater();
                CategoryTemplate template 
=   new  CategoryTemplate(currentLevel);

                rep.ItemTemplate 
=  template;
                rep.ItemDataBound 
+=   new  RepeaterItemEventHandler(rep_ItemDataBound);

                rep.DataSource 
=  ds;
                rep.DataBind();
                item.Controls.Add(rep);
                currentLevel
-- ;

            }
        }

        
void  rep_ItemDataBound( object  sender, RepeaterItemEventArgs e)
        {
            DataRowView drv 
=  (DataRowView)e.Item.DataItem;
            HyperLink link 
=  (HyperLink)e.Item.FindControl( " innerLink " );
            
if  (link  !=   null )
            {
                link.Text 
=  drv[ " categoryname " ].ToString();
                link.NavigateUrl 
=   " ../search.aspx?c= "   +  drv[ " id " ].ToString();

                ListInnerCategory(e.Item, 
int .Parse(drv[ " id " ].ToString()));
            }
        }
到此,一个多级的树菜单就出来了。
也许我这里的应用不是一个好的解决方案。
我写这篇文章的目的主要是说明Repeater的动态创建问题。就像题目中说的。
希望对你有所帮助。

转载于:https://www.cnblogs.com/mcsm/articles/1832653.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值