我有这个Web控件,我已经动态添加控件,但现在的要求是在控件周围添加一个有序列表.
要渲染控件,我将控件添加到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)); }
这是我要构建的html aspnet ctrls:
<fieldset> <ol> <li> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </li> <li> <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox> </li> </ol> </fieldset>
如何将控件放在列表项中?
我需要解决问题吗?
解决方法
更改此行:
controls.ToList().ForEach(c => Controls.Add(c));
对这些行:
Control ol = new HtmlGenericControl("ol"); controls.ToList().ForEach(c => ol.Controls.Add(new HtmlGenericControl("li").Controls.Add(c))); Controls.Add(ol);
编辑:
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);