zoukankan      html  css  js  c++  java
  • Repeater动态加载模版

    最近项目中用到了Repeater的动态加载模版,园子里找了找,发现一篇:http://www.cnblogs.com/xingshao/archive/2010/08/06/1793827.html

    参考了下,修改成自己需要的样子,在这记录下。

    public class MyTemplate:ITemplate
        {
            List<string> Mylist;
    
            public MyTemplate(Repeater rpt, List<string> list)
            {
                Mylist = list;
                rpt.ItemDataBound += new RepeaterItemEventHandler(rep_ItemDataBound);
            }
    
            public void InstantiateIn(Control container)
            {
                HtmlGenericControl tr = new HtmlGenericControl("tr");
                CheckBox chk = new CheckBox();
                chk.ID = "chk";
                HtmlGenericControl td1 = new HtmlGenericControl("td");
                td1.Controls.Add(chk);
                tr.Controls.Add(td1);
    
                HtmlGenericControl td2;
                foreach (var str in Mylist)
                {
                    Literal lbl = new Literal();
                    lbl.Text = str;
                    lbl.ID = "lbl_" + str;
    
                    td2 = new HtmlGenericControl("td");
                    td2.Controls.Add(lbl);
                    tr.Controls.Add(td2);
                }
                container.Controls.Add(tr);
            }
    
            void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                foreach (var str in Mylist)
                {
                    Literal lbl = (Literal)e.Item.FindControl("lbl_" + str);
                    lbl.Text = DataBinder.Eval(((RepeaterItem)lbl.NamingContainer).DataItem, lbl.Text).ToString();
                }
            }
        }
    
    后台调用:
    repWOList.ItemTemplate = new MyTemplate(repWOList, listTemp);
    repWOList.DataSource =List; repWOList.DataBind();
  • 相关阅读:
    centos安装composer
    fmt.Printf()
    php进程,线程,异步
    php异步处理
    php安装swoole扩展
    冒泡排序
    快速排序
    php的foreach指针
    无密钥登陆
    ubuntu18.04切换阿里云源
  • 原文地址:https://www.cnblogs.com/lvcao2099/p/1981189.html
Copyright © 2011-2022 走看看