Can I dynamically add HTML within a div tag from C# on load event?

164,057

Solution 1

You can add a div with runat="server" to the page:

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

and then set its InnerHtml property from the code-behind:

myDiv.InnerHtml = "your html here";

If you want to modify the DIV's contents on the client side, then you can use javascript code similar to this:

<script type="text/javascript">
    Sys.Application.add_load(MyLoad);
    function MyLoad(sender) {
        $get('<%= div.ClientID %>').innerHTML += " - text added on client";
    }
</script>

Solution 2

Use asp:Panel for that. It translates into a div.

Solution 3

Remember using

myDiv.InnerHtml = "something";

will replace all HTML elements in myDIV. you need to append text to avoid that.In that this may help

myDiv.InnerHtml = "something" + myDiv.InnerText;

any html control in myDiv but not ASP html controls(as they are not rendered yet).

Solution 4

You could reference controls inside the master page this way:

void Page_Load()
{
    ContentPlaceHolder cph;
    Literal lit;

    cph = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

    if (cph != null) {
        lit = (Literal) cph.FindControl("Literal1");
        if (lit != null) {
            lit.Text = "Some <b>HTML</b>";
        }
    }

}

In this example you have to put a Literal control in your ContentPlaceholder.

Solution 5

You want to put code in the master page code behind that inserts HTML into the contents of a page that is using that master page?

I would not search for the control via FindControl as this is a fragile solution that could easily be broken if the name of the control changed.

Your best bet is to declare an event in the master page that any child page could handle. The event could pass the HTML as an EventArg.

Share:
164,057

Related videos on Youtube

Sara Chipps
Author by

Sara Chipps

Director of Community at Stack Overflow. JavaScript developer, making Jewelbots, made Girl Develop It.

Updated on April 12, 2020

Comments

  • Sara Chipps
    Sara Chipps about 4 years

    Mind you, I am using master pages, but can I locate a div within the page and throw some html in there? Thanks.

    • JB King
      JB King over 15 years
      Are you talking about within a page or control and of which on the server or client side? There are a few different sides to this, I think but generally the answer is yes though YMMV.
  • Eon
    Eon almost 10 years
    Thanks a ton. Deadline of app moved up, really needed this code. thanks again
  • aspiring
    aspiring almost 9 years
    I used your approach and used it quite well with a stringbuilder and returning a serialized string to js side. Do you mind if I add it into your answer. Because without the serialization certain double quoted strings will not be properly transferred from C# to js.
  • Sandip Subedi
    Sandip Subedi over 7 years
    Does this still works ? I tired and it doesn't work for me.
  • Dov Miller
    Dov Miller over 5 years
    Can give an example?