WebBrowser - changing HTML content after DocumentCompleted event

R

Robson Siqueira

Folks,

I am facing a problem. I am trying to manipulate the HTML data (thru the
Document and DocumentText properties) of the WebBrowser object
(System.Windows.Forms).

The problem is that the application enters in a loop state and my changes
don't apply.

Have somebody faced the same problem? Any solutions?

I am using C# / .Net 2.0.
 
B

Ben Rush

Hi.

I've done a lot of code that resuses the IE control. I'm not sure what you
mean by "the application enters in a loop state", though. You mean your code
goes into a tight loop and the app freezes or what? If so, don't do that...
:)

Can you give me an example of exactly what you're trying to change/do?
Perhaps something I can recreate on my end/write a quick piece of code as an
example for you?
 
R

Robson Siqueira

Actually I want to change the code after it is loaded, which happens after
the DocumentCompleted Event. The problem is that the application keeps
loading the file - if you change it goes all the way over again - and so on.

Do you have any example in which you manipulate the HTML code AFTER the
document was loaded?

Thanks!
 
J

Jon Davis

Sounds like you might be having the same problem I had.

Click on this:
news://msnews.microsoft.com/[email protected]

If your problem is similar to mine, then to get the DocumentComplete when it
loads, reference shdocvw (find the COM object DLL in
C:\Windows\System32\shdocvw.dll and reference it in your .NET app) and cast
your ((System.Windows.Forms.WebBrowser)webBrowser1).ActiveXObject to a
SHDocVw.WebBrowser variable. Then subscribe to the DocumentComplete event of
the COM object, rather than the DocumentComplete event of the Windows Forms
instance.

shdocvw.WebBrowser _ActiveXBrowser = null;
public shdocvw.WebBrowser ActiveXBrowser
{
get
{
if (_ActiveXBrowser == null)
{
_ActiveXBrowser =
(SHDocVw.WebBrowser)myWebBrowser.ActiveXInstance;
}
return _ActiveXBrowser;
}
}

private void InitBrowser()
{
_ActiveXBrowser.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(ActiveXBrowser_DocumentComplete);
}


To avoid the re-load loop after changing the document content, set a flag in
your event handler to ignore the event if you just changed something.

Jon
 
J

Jon Davis

Also if you're dynamicizing the content, try to use the IE-DHTML DOM first,
i.e. myBrowser.Document.Body.InnerHtml = ..., rather than
myBrowser.DocumentText = .... Or, use
((mshtml.HTMLDocument)myBrowser.Document.DomDocument).body.innerHTML=...
(with mshtml.dll referenced).

Jon
 

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