Adding Html from Code Behind in Asp.net

Solution 1:

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

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

Solution 2:

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();
question.Text = "text";
divcontrol.Controls.Add(question); // add to the new div, not to the panel

Solution 3:

  1. Take one local string variable TEMP.
  2. Create same html as you want to display on screen and store it in variable TEMP.
  3. You can take html creation of control in separate function based on requirement.
  4. Place that created html as innerHTML to your panel/div.

That's it...

Solution 4:

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

Div1.InnerText = "Text";