Search code examples
c#asp.nethtmllabelweb-controls

Add a paragraph using Control.Add on Asp.net


So i have a Div, and on that div i add a label, i do some operations and then i add another, the problem is that the "paragraph" using the method Control.Add doesnt occur.
.
I get something like: "Label1""Label2".
instead of: .
"Label1".
"Label"

Heres the code that i use:

    System.Web.UI.WebControls.Label box3 = new System.Web.UI.WebControls.Label();
    box3.Text = TextBox2.Text;
    box3.ID = TextBox2.Text;
    oportunities.Controls.Add(box3);
    //operations
    oportunidades.Controls.Add(box4);

I tried to add "trash" to do the paragraph but so far no good


Solution

  • You need to add a LineBreak <br /> between the two texts:

    You can do this with a Literal-Control:

    oportunities.Controls.Add(box3);
    oportunities.Controls.Add(new LiteralControl("<br/>"));
    oportunidades.Controls.Add(box4);
    

    Or by simply appending <br /> to the first label:

    box3.Text = TextBox2.Text + "<br />";
    oportunities.Controls.Add(box3);
    oportunidades.Controls.Add(box4);