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?
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);
I would suggest to create a tree of HtmlGenericControls: http://msdn.microsoft.com/library/system.web.ui.htmlcontrols.htmlgenericcontrol.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With