从 Asp.net 中的代码隐藏添加 Html [英] Adding Html from Code Behind in Asp.net
本文介绍了从 Asp.net 中的代码隐藏添加 Html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将这样的 HTML 结构和控件从后面的代码添加到面板中
名<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>名<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label>名<asp:Label ID="lblFirstName" CssClass="ronly" runat="server"></asp:Label><asp:Panel runat="server" CssClass="sxpnl" ID="pnlUserdata"></asp:面板>
如果我尝试这样添加
HtmlGenericControl divcontrol = new HtmlGenericControl();divcontrol.Attributes["class"] = "sxro sx1co";divcontrol.TagName = "div";pnlUserSearch.Controls.Add(divcontrol);标签问题 = new Label();questionDescription.Text = "文本";pnlUserSearch.Controls.Add(问题);
它会一个接一个地添加控件,我怎样才能让控件像上图那样嵌套.
解决方案
不要将该子控件添加到面板中,而是将其添加到应该为父控件的控件中:
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
这篇关于从 Asp.net 中的代码隐藏添加 Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文