动态创建Repeater,绑定数据

动态创建Repeater,绑定数据有一个菜单树,因为界面上的限制(要求)不能使用现有的控件。那我就想到了用Repeater控件自己来写,可以灵活、方便的控制页面。<asp:RepeaterID=”CategoryList”runat=”server”OnItemDataBound=”CategoryList_ItemDataBound”><HeaderTemplate>…

大家好,欢迎来到IT知识分享网。

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

那我就想到了用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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23756.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信