Johnno Nolan
Johnno Nolan

Reputation: 29659

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?

Upvotes: 10

Views: 13920

Answers (2)

matt-dot-net
matt-dot-net

Reputation: 4244

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

Upvotes: 11

onof
onof

Reputation: 17367

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

Upvotes: 6

Related Questions