Smith
Smith

Reputation: 5951

c# access dom javascript generated form

am loading a webpage using the webbrowser control, in it there is a form that is generateb by javascript.

when i tried to access th elements in c#, they cannot be found.

the page renders well in a normal browser, but in my winforms app, the form elements are not rendered, i can only see the javascript that generated the form when dubugging

a close solution i found on stack was to cast the webbrowser.document.domdocument to ihtmldocument, but i havent been successful with it,

this is what am trying to do

Dim doc As HtmlDocument = DirectCast(wbMain.Document.DomDocument, MSH.IHTMLDocument)

but i get this warning

Runtime errors might occur when converting 'mshtml.IHTMLDocument' to 'System.Windows.Forms.HtmlDocument'.

and whe i ignore and run, i got this error

Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to class type 'System.Windows.Forms.HtmlDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

EDIT A sample javascript

<SCRIPT type=text/javascript>  
        (function(){            
        function runElemGen() {
       var ElemGenForm = [{"label":"Email:","name":"ElemGen_email_field","oldName":"","value":"","ElemGen":"text"},{"label":"","name":"ElemGensubscribe","oldName":"","value":"Submit","ElemGen":"submit"}];
       ElemGenBuildForm({makeDivs: true, arr:ElemGenForm, place:"ElemGen-email-form-"});                
        }
      if (window.addEventListener) {
        window.addEventListener("load", runElemGen, false);
      } else {
        window.attachEvent("onload", runElemGen);
      }
    })();
</SCRIPT>

can anyone with a solution help

Upvotes: 3

Views: 992

Answers (2)

Erx_VB.NExT.Coder
Erx_VB.NExT.Coder

Reputation: 4856

You did say the generated form is not displaying in your webbrowser control, so I am guessing it is displaying in your normal Internet Explorer session, as I correct? Do you realize that when you run the WebBrowser Control, it runs under the IE 7.0 rendering engine? This is done for compatibility. So, if there is anything in your JS that requires IE version 8.0 or above, it will not work in the webbrowser control unless you specifically change some registry settings (or do a metadata tag insertion) indicating that you want the content to render under the currently installed version of IE, or a particular fixed version of your choosing.

If this is the problem, my answer to the following question will almost certainly solve your problem. You can find it here: Script runs slower in the dotnet WebBrowser control

Let me know how it goes, and if it solves your problem, if it doesn't, I'll work with you until we find a solution.

Upvotes: 0

marr75
marr75

Reputation: 5715

Edit: The solution below fixes the OP's inability to access the dom for debugging purposes. The OP actually needs to solve the problem of the document not appearing as expected (or at all) in the WebBrowserControl. The WebBrowserControl is a repackaging of the ActiveX control of the same name and so will probably use the currently installed version of Internet Explorer. If possible, I suggest launching IE on the computer in question and attempting to render the same document, including the code used to load the document in your WebBrowserControl would also help us diagnose the problem.

I think you want something along the lines of:

if (webBrowser1.Document != null)
{
    var currentDoc = (MSHTML.IHTMLDocument2)webBrowser1.Document.DomDocument;
    return (currentDoc.lastModified);
}

Your conversion from IHTMLDocument to HtmlDocument is invalid.

Here's the IHTMLDocument2 property and method docs.

Expanding on that, in the above example, webBrowser1.Document is a System.Windows.Forms.HtmlDocument. Its DomDocument property is an unmanaged pointer to the document. It's only needed when you are looking for properties of the document that are not exposed on the managed Document property of the browser control.

Upvotes: 3

Related Questions