Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an unordered list with asp.net controls?

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?

like image 844
Johnno Nolan Avatar asked Jul 18 '10 20:07

Johnno Nolan


2 Answers

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);

EDIT:

    Control ol = new HtmlGenericControl("ol");
    controls.ToList().ForEach(c =>
                                  {
                                      var li = new HtmlGenericControl("li");
                                      li.Controls.Add(c);
                                      ol.Controls.Add(li);
                                  });
    Controls.Add(ol);
like image 91
matt-dot-net Avatar answered Sep 30 '22 12:09

matt-dot-net


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

like image 42
onof Avatar answered Sep 30 '22 11:09

onof