WebBrowser DocumentCompleted and Print()

E

esw

Hi,

I am using the WebBrowser control in a winform app (.NET 2.0, WinXP
SP2). I am loading a local file which contains Javascript which
dynamically modifies the layout of the page when it loads. When I set
the URL to this file and then call Print(), it prints correctly
(Javascript is executed). When I call Print() in DocumentCompleted, it
prints incorrectly (as though the Javascript is not executed) even
though the page displays correctly in the embedded browser. I have
tried testing for IsBusy and WebBrowserReadyState.Complete but it does
not make a difference. Does anyone understand why this is happening? Am
a forgetting to set something? I am wondering if its a security
issue... that in DocumentCompleted the document is perceived as
belonging to the wrong zone.

// prints correctly
this.webBrowser2.Url = new Uri(@"C:\myfile.html");
this.webBrowser2.Print();

// prints incorrectly
private void btnPrint_Click(object sender, EventArgs e)
{
this.webBrowser2.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);
this.webBrowser2.Url = new Uri(@"C:\myfile.html");
}

void webBrowser2_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
if (this.webBrowser2 != null)
{
this.webBrowser2.Print();
}
}

Thanks!
- esw
 
E

esw

Found the answer... this forces the Javascript to execute:

void webBrowser2_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument objHtmlDoc = this.webBrowser2.Document as
HtmlDocument;
objHtmlDoc.InvokeScript("OnLoad");
this.webBrowser2.Print();
}

Thanks,
-esw
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top