tags:

views:

62

answers:

4

When i try to insert html text into a div (i made it runat=server and i populate the div with html text. it's look like this,

string htmlData = "< html > blabla...< /html >";
divcontent.innerHtml = htmlData;

the page is damaged.

but when i take the same html data and put it as is in an html file its shows properly. what should i take in consider ?

p.s. i am writing in asp.net (c#) VS2005.

+3  A: 

HTML is the root element and you are only allowed to have it once.

See here.

Try replacing <html> with a <div>

string htmlData = "<div> blabla...< /div>"; 
divcontent.innerHtml = htmlData;

Based on the comment. This is how you can use an IFrame and set it. I have no idea if this works in all broswers. My advice is rethink your solution to make sure you are required to have all the header information.

<html>

    Hello this will have an IFrame

    <br /><br /><br />

    <iframe id="myFrame">
        If you see this message get a new broswer.
    </iframe>

    <script>

        var myFrame = document.getElementById('myFrame');

        if(myFrame != null)
        {
            myFrame.contentWindow.document.write('<html><span>Hello World</span><html>');
        }
        else
        {
            alet('Did not find FRAME');
        }

    </script>

</html>
David Basarab
thank you for your reply, it's still dont work. i have also an HEAD elements and body elements is it also making the problem?i tried to used iframe insted but it doesnt show the innerHtml.it's looks like this:string htmlData = "<html > blabla...< /html>"; iframecontent.innerHtml = htmlData;
gadym
@gadym - See my Edit.
David Basarab
A: 

Could you paste what the "blabla...." is? If you are trying to paste a whole HTML document (with HEAD, BODY tags etc) into a div it might do something weird to your page. You should be putting an HTML fragment into the div. Is that what you are doing?

Abe Miessler
it did something weird to my page.i want to prevent it.i want to show an html page but i get it as string in to my own div or iframe. (the one who can works, i dont mind).
gadym
A: 

I don't believe you can use HTML like that, since the DIV is inside the body already.

You will need to split up the string into the correct HTML tags..

pull out the header and put it into a header string and same with the body

I've done something similar, but I saved the Header tags into a string and body in a seperate string then displayed them in the proper places

LeeHull
i'm geting the html string from another source (let say, webservice). i cant modify the html string i received. so i need to put this data as is to my own div or iframe.
gadym
A: 

i found the soultion to my problem. i needed to remove the DOCTYPE tag from my page. it causes a contradiction with my html string. i dont know what to replace it with but it works for me...

gadym
You have turned moved your page out of "Standards mode" and into "Quirks mode" this causes browsers to perform even more error correction (to cope with authors who do stupid things like putting HTML elements inside DIV elements) and causes major inconsistencies between browsers (such as Internet Explorer getting the meaning of `width` wrong).
David Dorward
what other choices do i have ?i cant manipulate the html string i get.i cant save the html string into file and show it on another page.i have limited options...
gadym