Search code examples
c#iframeembedwebbrowser-control

Embedded iframe in WebBrowser Control not loading


I have the following code:

webBrowser1.Navigate("about:blank");

string _embeddedpage = "<html><head></head><body bgcolor=\"black\"><iframe height=\"300\" width=\"600\" src=\"http://www.youtube.com/embed/9bZkp7q19f0\"></iframe></body></html>";

webBrowser1.Document.Write(_embeddedpage);

Unfortunately no matter what I try the iframe doesn't load. The HTML works fine if I save it out to a HTML file and then run it, and it also works fine if i then point the webBrowser control at the physical html file.

However when doing it like this and creating the HTML from within the application it just won't play ball.


Solution

  • I have just tried it myself. This code will work for you:

    private void Form1_Load(object sender, EventArgs e) 
    {
        string _embeddedpage = @"
                <html>
                    <body>
                        <iframe class='youtube-player' type='text/html' width='640' height='385'
                            src='http://www.youtube.com/embed/9bZkp7q19f0\' frameborder='0'>
                        </iframe>
                    </body>
                <html>";
    
        webBrowser1.DocumentText = _embeddedpage;
    }