Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have created table run time which is in Div.And after i click on button i want inner HTML of div but it givs me error "Cannot get inner content of Div Table because the contents are not literal".

What should i Do?
Posted
Comments
Manfred Rudolf Bihy 13-May-11 8:46am    
Please show us some code. Strip it down to the stuff nescessary to reproduce the error. Edit your question using the link "Improve question".

Thank you!

in javascript you can try like this

document.getElementById('Divname').innerHTML;
 
Share this answer
 
I tried to reproduce your error by using two methods of creating the table within the div. I only found out one difference: In IE using innerHTML to set the table construct one can omit the TBODY tag. It will be created by IE on its own, but when you create the table via createElement and appendChild you MUST include the TBODY tag otherwise the table wont be shown. FF works happily with both variants.

Enough said, here's the code to corroborate my claim:

XML
<html>
<head>

<script type="text/javascript">
function buildTableWithInnerHTML()
{
    var c = document.getElementById("container1");
    c.innerHTML = "<table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></table>";
}

function buildTableWithCreateElement()
{
    var d = document;
    var c = d.getElementById("container2");
    var t  = d.createElement("table");
    var tb = d.createElement("tbody");

    c.appendChild(t);
    t.appendChild(tb);

    var r1 = d.createElement("tr");
    var r2 = d.createElement("tr");

    tb.appendChild(r1);
    tb.appendChild(r2);

    var d11 = d.createElement("td");
    var d12 = d.createElement("td");
    var d13 = d.createElement("td");
    var d21 = d.createElement("td");
    var d22 = d.createElement("td");
    var d23 = d.createElement("td");
    d11.appendChild(d.createTextNode("1"));
    d12.appendChild(d.createTextNode("2"));
    d13.appendChild(d.createTextNode("3"));
    d21.appendChild(d.createTextNode("4"));
    d22.appendChild(d.createTextNode("5"));
    d23.appendChild(d.createTextNode("6"));
    r1.appendChild(d11);
    r1.appendChild(d12);
    r1.appendChild(d13);
    r2.appendChild(d21);
    r2.appendChild(d22);
    r2.appendChild(d23);
}
</script>

<script type="text/javascript">

function ShowDivInnerHTML()
{
    var c = document.getElementById('container1');
    alert("Created by innerHTML:\n" + c.innerHTML);
    c = document.getElementById('container2');
    alert("Created by create & append:\n" + c.innerHTML);
}

</script>

<title>Dynamic Table in DIV</title>
</head>
<body onload="buildTableWithCreateElement();buildTableWithInnerHTML();">
<h2>Generated by setting innerHTML</h2>
<div id="container1"></div>
<h2>Generated via createElement & appendChild</h2>
<div id="container2"></div>
<input type="button" value="Show it!" onClick="ShowDivInnerHTML();">
<br>
</body>
</html>


In case you should still have some doubts feel free to leave a comment.

Happy coding!

-MRB
 
Share this answer
 
Comments
prathamesh812 16-May-11 1:52am    
Thanx for suggestion,But i want that div at code behind file i.e. in cs file and store into that particular html object.
Manfred Rudolf Bihy 17-May-11 8:19am    
Which part of my example are you not understanding?
It doesn't really matter where the HTML comes from be it generated in the codebehind file, rendered to the browser or dynamically generated in the browser. It is all the same. Besides that you ignorant fool I already asked you for your code that would help us to reproduce your problem which you ignored.

So please help yourself, as I'm done with you, bye!
var sb = new StringBuilder();
mainDiv.RenderControl(new HtmlTextWriter(new StringWriter(sb)));

string s = sb.ToString();
 
Share this answer
 
Comments
thatraja 9-Jun-12 3:59am    
Please don't dig old questions. Here this question already with 2 answers. Leave it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900