快速业务通道

动态创建Repeater,绑定数据

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-05-20

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

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

();

    

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号