ASP.NET Dynamic Repeater in C#

public class RepeatingRuleTemplate : ITemplate {     ListItemType templateType; List innerControls;     public RepeatingRuleTemplate(ListItemType type, List<Control> controls)    {        templateType = type;        innerControls = controls;    }    public void InstantiateIn(Control container)    {        PlaceHolder ph = new PlaceHolder();        switch (templateType)        {            case ListItemType.Header:                ph.Controls.Add(new LiteralControl("<table border="0">"));                ph.Controls.Add(new LiteralControl("<tr>"));                foreach (Control control in innerControls)                {                    Label label = new Label();                    label.Text = control.ID;                    ph.Controls.Add(new LiteralControl("<td>"));                    ph.Controls.Add(label);                    ph.Controls.Add(new LiteralControl("</td>"));                }                ph.Controls.Add(new LiteralControl("</tr>"));                break;            case ListItemType.Item:                ph.Controls.Add(new LiteralControl("<tr>"));                foreach (Control control in innerControls)                {                        //ph.Controls.Add(new LiteralControl("<td>"));                        //ph.Controls.Add(control as TextBox);                        //ph.Controls.Add(new LiteralControl("</td>"));                    if (control.GetType() != typeof(Repeater))                    {                        ph.Controls.Add(new LiteralControl("<td>"));                        TextBox textBox = new TextBox();                        textBox.ID = control.ID;                        ph.Controls.Add(textBox);                        ph.Controls.Add(new LiteralControl("</td>"));                    }                    else                    {                        ph.Controls.Add(new LiteralControl("<td>"));                        Repeater rpt = new Repeater();                        rpt.DataSource = (control as Repeater).DataSource;                        rpt.ItemTemplate = (control as Repeater).ItemTemplate;                        rpt.HeaderTemplate = (control as Repeater).HeaderTemplate;                        rpt.FooterTemplate = (control as Repeater).FooterTemplate;                        rpt.DataBind();                        ph.Controls.Add(rpt);                        //(control as Repeater).DataSource = new DataRow[4];                        //   (control as Repeater).DataBind();                        ph.Controls.Add(new LiteralControl("</td>"));                    }                }                ph.Controls.Add(new LiteralControl("</tr>"));                //ph.DataBinding += new EventHandler(Item_DataBinding);                break;            case ListItemType.Footer:                ph.Controls.Add(new LiteralControl("</table>"));                break;        }        container.Controls.Add(ph);    }    public List<Control> Controls    {        get        {            return innerControls;        }    }}

Usage:

protected void Page_Load(object sender, EventArgs e){    CreateNestedRepeater();}private void CreateNestedRepeater(){    Repeater childRpt = new Repeater();    List repeatingRuleControls = new List();    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    RepeatingRuleTemplate repeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);    childRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);    childRpt.ItemTemplate = repeatingRuleTemplate;    childRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);    childRpt.DataSource = new DataRow[4];    Repeater parentRpt = new Repeater();    repeatingRuleControls = new List();    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(new TextBox());    repeatingRuleControls.Add(childRpt);    RepeatingRuleTemplate parentrepeatingRuleTemplate = new RepeatingRuleTemplate(ListItemType.Item, repeatingRuleControls);    parentRpt.HeaderTemplate = new RepeatingRuleTemplate(ListItemType.Header, repeatingRuleControls);    parentRpt.ItemTemplate = parentrepeatingRuleTemplate;    parentRpt.FooterTemplate = new RepeatingRuleTemplate(ListItemType.Footer, null);    parentRpt.DataSource = new DataRow[4];    parentRpt.DataBind();    phControls.Controls.Add(parentRpt);[]}

Resources: Stack Overflow, msdn

Leave a Reply

Your email address will not be published. Required fields are marked *