简体   繁体   中英

Error calling C# method from JS in winforms webbrowser control with parameters

I am attempting to call a C# method on my winform from a page opened in a web browser control similar to this:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.objectforscripting%28v=vs.110%29.aspx

On my winform I have this method:

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }

and on my web page I am calling: window.external.Test('called from script code');

When I attempt to call this I get a javascript error: "invalid procedure call or argument"

However when I modify my form's method to take no parameter (like public void Test() ) and accordingly make the javascript call without the parameter, it works without any issue. I only have the issue when I am attempting to pass parameters (which I need to do).

more info: I do have the following attributes on my form class:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]

and I am assigning the ObjectForScripting:

webBrowser1.ObjectForScripting = this;

When debugging it does not appear to enter the forms method block.

I must be missing something simple.

Dumb mistake. I was evaluating that the function was not undefined as you normally would before calling a function from some other part of code. For some reason this evaluation was where the error was being raised, not the call itself (that was actually ok).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM