在Asp.net中从代码后面添加Html

时间:2021-07-24 13:17:51

I want to add HTML structure and control like this from code behind into a panel

我想将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>

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.

它将一个接着一个地添加控件,如何让控件像上面所示那样嵌套。

5 个解决方案

#1


12  

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

#2


20  

For appending HTML to your panel, add a LiteralControl control to your panel:

要向面板添加HTML,请在面板中添加文字控制控件:

string yourHTMLstring = "<div class='Main'>....";
pnlUserdata.Controls.Add(new LiteralControl(yourHTMLstring));

#3


1  

<div id="Div1" runat="server">

Div1.InnerText = "Text";

#4


0  

Make the div runat="server"

使div runat = "服务器"

<div id="d" runat="server"></div>

and add the controls inside div like below

并在下面的div中添加控件。

d.Controls.Add();

#5


0  

  1. Take one local string variable TEMP.
  2. 取一个局部字符串变量TEMP。
  3. Create same html as you want to display on screen and store it in variable TEMP.
  4. 创建您希望在屏幕上显示的相同的html并将其存储在变量TEMP中。
  5. You can take html creation of control in separate function based on requirement.
  6. 您可以根据需要在单独的函数中创建html控件。
  7. Place that created html as innerHTML to your panel/div.
  8. 将创建的html作为innerHTML放置到面板/div中。

That's it...

就是这样……

#1


12  

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

#2


20  

For appending HTML to your panel, add a LiteralControl control to your panel:

要向面板添加HTML,请在面板中添加文字控制控件:

string yourHTMLstring = "<div class='Main'>....";
pnlUserdata.Controls.Add(new LiteralControl(yourHTMLstring));

#3


1  

<div id="Div1" runat="server">

Div1.InnerText = "Text";

#4


0  

Make the div runat="server"

使div runat = "服务器"

<div id="d" runat="server"></div>

and add the controls inside div like below

并在下面的div中添加控件。

d.Controls.Add();

#5


0  

  1. Take one local string variable TEMP.
  2. 取一个局部字符串变量TEMP。
  3. Create same html as you want to display on screen and store it in variable TEMP.
  4. 创建您希望在屏幕上显示的相同的html并将其存储在变量TEMP中。
  5. You can take html creation of control in separate function based on requirement.
  6. 您可以根据需要在单独的函数中创建html控件。
  7. Place that created html as innerHTML to your panel/div.
  8. 将创建的html作为innerHTML放置到面板/div中。

That's it...

就是这样……