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

Sara Chipps picture Sara Chipps · Jan 21, 2009 · Viewed 159.7k times · Source

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

Answer

M4N picture M4N · Jan 21, 2009

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>