Firing client-scripted events.

Ev Conrad

Newcomer
Joined
Jan 13, 2003
Messages
17
I have a .NET application that hosts a web browser
control. I load the document body with a web page using
the following line of code:

Body.innerHTML = content;

Easy enough, but how can I fire events that are scripted
into the web page? For instance, if there is an 'OnLoad'
event - how can I make it fire so that dynamic changes
that are supposed to occur actually happen? Thanks in
advance!

Everett
 
Thanks for the reply. Rather than have code fire when the download is complete, or for other methods of the web browser, I am trying to sink the client side script. For instance - if the HTML document has an 'onload' event handler, I would like this code to run when the document is loaded into the browser. I may have to load the document in a manner other than the way I'm currently doing it. That would be OK.

Everett
 
One option would be to write the HTML to a temporary file, and then
navigate to that local file, rather than simply setting the innerhtml.
 
This would solve this specific case, but I would like the possibility to sink other events as well. Also - persisting the stream to disk means that I have to encrypt it.

Everett
 
You're gonna love me...

A month or so ago, I was playing around with the properties of the AxWebBrowser control and found the .Document property

guess what?! the .Document property is the linking from the vb( errrrrrr should be same in c#, least you'd think so ) code environment to the html JAvaScript environment...

so for example:
Visual Basic:
Me.AxWebBrowser1.Document.all.write("<b>This is some bold text</b>")
will work just fine...

NOTE: the only catch is that at design time, you not only cant set the html file or url location for the AxWebBrowser control, but when you type in:
Visual Basic:
Me.AxWebBrowser1.Document.
ONLY the .GetType() option will appear... Additionally, the code you type in after the .Document will be in whatever case you use( hence, .all will be .all, not .All )

So you can't see the properties or method in your webpage, but Rest Assured, they are there and your code Will run them
;)

What do you think?
 
This doesn't seem to work for me. I have tried a variety of different variations of the above, including:

Body.Document.onload();
Body.Document.onload;
Body.Document.load;
Body.Document.load();
webControl.document.all.onload();
webControl.document.onload();

The compiler always complains that there is no such method. Let me know of anything I might have missed. I have to overcome this issue for a project, and I think your post puts me on the right track. Thanks again!

Everett
 
just off the top of my head, try:
document.styles.onload="MyEventFunctionNAme();"

btw, how do you get the Body.innerHTML to come up?

Al I have done is create an AxWebBrowser object and use the AxWebBrowser.Document object to access the DOM in the webpage...of course, I am unable to access custom methods and properties either...Also, I am unable to access the styles object through this process ( vb try to separate what I type due to some of the style object properties haveing dashes in them )

Are you using a regular form or an xml form?

Are you using an Imports or Inherits line to gain access to the DOM?
 
Here is a snippet of how I write to the browser control.

Navigate("about:blank");
while (body == null) Application.DoEvents();
body.innerHTML = Encoding.ASCII.GetString(htmlBuffer);

I'm sure that there are better ways of doing this. To access the DOM, I add a reference to my project and import the namespace:

using mshtml; // This is in c#.

Hope this helps. I haven't been able to fire the client events at will, but I will come back to this problem next week when I have a bit more time. Thanks again for your help!

Everett
 
Real quick....

This is what I would calla quick fix for the issue ( Im at work right now and havent access to my vb.net )...

try:
Visual Basic:
Me.AxWebBrowser.Document.write("<script>callMyFunctionHere();</script>")

it's kinda ghetto but int heory it should work for now...

try it and let me know how it ferrls in c#...
 
C# enforces strong typing of everything, VB.NET does not by default. Everyone writing VB.NET should go in to the project options and turn on Option Strict, which forces you to do things properly.

To strongly type the WebBrowser.Document property, you need to reference MSHTML and cast the property to HTMLDocument, or IHTMLDocument2 or whatever the type is.
 
Add a reference to Microsoft.mshtml. This will be in the list of .NET assemblies to reference in the Add Reference dialog. Then stick a webbrowser control on your form, let's call it "wb" for this example. Do a wb.Navigate("www.google.com") in the Form Load to set it up on a website.

You can then use code like the following to access the HTML document with full intellisense support, and enforcing strong typing all the way:

Visual Basic:
Dim doc As mshtml.IHTMLDocument2

doc = DirectCast(wb.Document, mshtml.IHTMLDocument2)
doc.location.href = "http://www.microsoft.com"

C#:
mshtml.IHTMLDocument2 doc;

doc = (mshtml.IHTMLDocument2)wb.Document;
doc.location.href = "http://www.microsoft.com";
 
Rock On!!! :)

Sweeet!! Divil, thank you very very very much!!!!

This answers the question for Both of us far better than I had hoped...

For those reading this and looking for the reference Divil mentioned, I found it at:

Solution Explorer > ( Right-Click ) References > Add Reference > .NET Tab > ( Double-Click to add ) Microsoft.mshtml > Click on OK

Then the code Divil provided works perfectly with no errors :D


Thanx Again, Divil!!
 
Thanks again for everyone's support. I have been able to get this far - my challenge is to sink the events that fire in the HTML page loaded in the browser control. For instance, if a page has a JavaScript or VBScript 'onload' event - how can I sink this in the browser so that I can do certain things when certain page events fire? I will have complete control over the pages, so I will know what events wuold need to be supported, but a general-use case is welcome.

Everett
 
Hopefully this sample I knocked up will help. It demonstrates hooking up HTML events to C# event handlers. This sample hooks up the onmouseover event, and displays the text of the element the user is hovering over in a label on the form.
 

Attachments

It's almost exactly the same for VB.NET; C# and VB.NET are the
same language, essentially; the main difference being the syntax.
In this particular example, the syntax isn't even that much different.

Even a basic understanding of the structure of the C# languages should
be enough for you to port it over yourself.
 
Really? Niice...

I thought c# was a whole new language totally different from everything we're used to but made somehow simple...

I always figured it would be a somewhat dificult to learn, do you think it is, VolteFace??
 
Back
Top