Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 858
Sara Chipps Avatar asked Jan 21 '09 21:01

Sara Chipps


People also ask

How to add HTML element dynamically?

With document. createElement() method you can create a specified HTML element dynamically in JavaScript. After creation you can add attributes. If you want the element to show up in your document, you have to insert in into the DOM-tree of the document.

How dynamically add HTML controls in asp net?

You can set the InnerHtml attribute of just about any control. Whenyou need to have dynamic HTML, better to have a div in aspx page with an id and runat="server" then edit the HTML as You need in code-behind.


2 Answers

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> 
like image 75
M4N Avatar answered Oct 05 '22 03:10

M4N


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

like image 21
Otávio Décio Avatar answered Oct 05 '22 02:10

Otávio Décio