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

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


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>


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


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).