Ev Conrad Posted January 22, 2003 Posted January 22, 2003 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 Quote
UCM Posted January 22, 2003 Posted January 22, 2003 Do you mean in HTML, JavaScript, or VB.NET? If you're using VB.NET, you can have VB code execute when the document finishes downloading into the AxWebBrowser control... See the below image: Quote UCM >-)
Ev Conrad Posted January 22, 2003 Author Posted January 22, 2003 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 Quote
Ev Conrad Posted January 22, 2003 Author Posted January 22, 2003 BTW - I am using C#, although it should be the same concept for VB. Everett Quote
*Experts* Volte Posted January 22, 2003 *Experts* Posted January 22, 2003 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. Quote
Ev Conrad Posted January 22, 2003 Author Posted January 22, 2003 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 Quote
UCM Posted January 22, 2003 Posted January 22, 2003 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: 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: 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? Quote UCM >-)
Ev Conrad Posted January 22, 2003 Author Posted January 22, 2003 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 Quote
UCM Posted January 22, 2003 Posted January 22, 2003 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? Quote UCM >-)
Ev Conrad Posted January 23, 2003 Author Posted January 23, 2003 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 Quote
UCM Posted January 23, 2003 Posted January 23, 2003 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: 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#... Quote UCM >-)
*Gurus* divil Posted January 23, 2003 *Gurus* Posted January 23, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
UCM Posted January 25, 2003 Posted January 25, 2003 Niiice, Divil... Hey, could you knock up an example of how to accomplish this? :) Quote UCM >-)
*Gurus* divil Posted January 25, 2003 *Gurus* Posted January 25, 2003 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: Dim doc As mshtml.IHTMLDocument2 doc = DirectCast(wb.Document, mshtml.IHTMLDocument2) doc.location.href = "http://www.microsoft.com" mshtml.IHTMLDocument2 doc; doc = (mshtml.IHTMLDocument2)wb.Document; doc.location.href = "http://www.microsoft.com"; Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
UCM Posted January 25, 2003 Posted January 25, 2003 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!! Quote UCM >-)
Ev Conrad Posted January 26, 2003 Author Posted January 26, 2003 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 Quote
*Gurus* divil Posted January 26, 2003 *Gurus* Posted January 26, 2003 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.htmlevents.zip Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
UCM Posted January 27, 2003 Posted January 27, 2003 Nice idea, Divil... Could you make an example for vb users :) Quote UCM >-)
*Experts* Volte Posted January 27, 2003 *Experts* Posted January 27, 2003 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. Quote
UCM Posted January 27, 2003 Posted January 27, 2003 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?? Quote UCM >-)
Moderators Robby Posted January 29, 2003 Moderators Posted January 29, 2003 Divil, very nice sample (HTML Events) Quote Visit...Bassic Software
Ev Conrad Posted January 29, 2003 Author Posted January 29, 2003 Divil, Thanks - this is just what I am looking for. I'll let you know when I get my project up & running with the client-scripted events. Everett Quote
aewarnick Posted April 27, 2003 Posted April 27, 2003 It look almost exacly like VB.Net to me. I think C# is less writing though. That is one of the reasons I chose it. Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.