我想从代码隐藏到面板
中添加这样的HTML结构和控件<div class='Main'>
<div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div>
<div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div> <div class='cst'>
First Name
</div>
<div class='csc'>
<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>
</div>
<div class='end'>
</div>
</div>
<asp:Panel runat="server" CssClass="sxpnl" ID="pnlUserdata">
</asp:Panel>
如果我尝试像这样添加
HtmlGenericControl divcontrol = new HtmlGenericControl();
divcontrol.Attributes["class"] = "sxro sx1co";
divcontrol.TagName = "div";
pnlUserSearch.Controls.Add(divcontrol);
Label question = new Label();
questionDescription.Text = "text";
pnlUserSearch.Controls.Add(question);
它将一个接一个地添加控件,如何使控件像上面所示那样嵌套。
答案 0 :(得分:24)
要将HTML添加到面板,请在面板中添加LiteralControl
控件:
string yourHTMLstring = "<div class='Main'>....";
pnlUserdata.Controls.Add(new LiteralControl(yourHTMLstring));
答案 1 :(得分:14)
不要将该子控件添加到面板中,将其添加到应该是父控件的控件中:
HtmlGenericControl divcontrol = new HtmlGenericControl();
divcontrol.Attributes["class"] = "sxro sx1co";
divcontrol.TagName = "div";
pnlUserSearch.Controls.Add(divcontrol);
Label question = new Label();
questionDescription.Text = "text";
divcontrol.Controls.Add(question); // add to the new div, not to the panel
答案 2 :(得分:1)
<div id="Div1" runat="server">
Div1.InnerText = "Text";
答案 3 :(得分:0)
制作div runat="server"
<div id="d" runat="server"></div>
并在div中添加控件,如下所示
d.Controls.Add();
答案 4 :(得分:0)
就是这样......