更新时间:2023-02-11 22:20:55
不要将该子控件添加到面板中,而是将其添加到应该为父控件的控件中:
HtmlGenericControl divcontrol = new HtmlGenericControl();divcontrol.Attributes[class"] =sxro sx1co";divcontrol.TagName = "div";pnlUserSearch.Controls.Add(divcontrol);标签问题 = new Label();questionDescription.Text = "text";divcontrol.Controls.Add(问题);//添加到新的 div,而不是面板
I want to add HTML structure and control like this from code behind into a panel
<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>
If i try to add like this
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);
It will add controls one after another, how can i make controls go nested like that as shown above.
Don't add that child control to the panel, add it to the control that should be the parent:
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