WebForm 动态生成Repeater并绑定数据

前几天,项目中需要实现动态生成Repeater并绑定数据的功能,记录一下实现的方法

一、使用模板实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepeaterDemo
{
    public class RepeaterTemplet : ITemplate
    {
        ListItemType templateType;//列表控件类型
        string headerText = string.Empty;//表头数据 html
        string columns = string.Empty;//绑定数据列名 格式"UserName,"
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public RepeaterTemplet(ListItemType type, string headertext, string columninfo)
        {
            templateType = type;
            headerText = headertext;
            columns = columninfo;
        }
        /// 
        /// 
        /// 
        /// 
        public void InstantiateIn(System.Web.UI.Control container)
        {
            Literal lc = new Literal();
            switch (templateType)
            {
                case ListItemType.Header:
                    lc.Text = headerText;
                    break;
                case ListItemType.Item:
                    lc.DataBinding += new EventHandler(TemplateControl_DataBinding);
                    break;
                case ListItemType.AlternatingItem:
                    lc.Text = "Item number:1";
                    break;
                case ListItemType.Footer:
                    lc.Text = "";
                    break;
            }
            container.Controls.Add(lc);
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        private void TemplateControl_DataBinding(object sender, System.EventArgs e)
        {
            Literal lc;
            lc = (Literal)sender;
            RepeaterItem container = (RepeaterItem)lc.NamingContainer;           
            string itemText = "";
            string []columnsInfo = columns.Split(',');
            for (int i = 0; i < columnsInfo.Length; i++)
            {
                itemText += "" + DataBinder.Eval(container.DataItem, columnsInfo[i]) + "";
            }
            itemText += "";
            lc.Text = itemText;
        }
    }
}

调用示例

   protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserID",typeof(string));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("Tel", typeof(string));
            for (int i = 0; i < 10; i++)
            {
                dt.Rows.Add((i+1).ToString(),"Test"+i,i.ToString());
            }
            string headerText = "";
            string columns = "UserID,UserName";
            rept.HeaderTemplate = new RepeaterTemplet(ListItemType.Header ,headerText,"");
            rept.ItemTemplate = new RepeaterTemplet(ListItemType.Item, "", columns);
            rept.FooterTemplate = new RepeaterTemplet(ListItemType.Footer, "", "");
            rept.DataSource = dt;
            rept.DataBind();
        }

二、使用控件asp:Literal实现

将数据拼接成html字符串(table),将html字符串赋值给该控件,在前台页面输出

//前台

//后台 testhtml为html格式的字符串
htmltext.Text=testhtml;

 

你可能感兴趣的:(.Net,Repeater,C#)

用户编号用户姓名