views:

138

answers:

2

I've got this web control that I've been dynamically adding controls to but now the requirement is to add an ordered list around the controls.

To render the controls I add the controls to ControlsCollection

    protected void Page_Load(object sender, EventArgs e)
    {
        var document = XDocument.Load(@"http://localhost:49179/XML/Templatek.xml");
        var builder = ObjectFactory.GetInstance<IControlBuilder>();
        var controls =builder.BuildControls(document);
        controls.ToList().ForEach(c => Controls.Add(c));

    }

And this is the html+aspnet ctrls I want to build:

 <fieldset>
    <ol>
      <li>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
      </li>
      <li>
          <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
      </li>
    </ol>
    </fieldset>

How do I position the controls in the list items? Do I need to approach the problem differently?

+2  A: 

I would suggest to create a tree of HtmlGenericControls: http://msdn.microsoft.com/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx

onof
+1  A: 

Change this line:

controls.ToList().ForEach(c => Controls.Add(c)); 

To these lines:

Control ol = new HtmlGenericControl("ol");
controls.ToList().ForEach(c => ol.Controls.Add(new HtmlGenericControl("li").Controls.Add(c)));
Controls.Add(ol);
matt-dot-net