GLOBEtech



Call Managed C# Method from JavaScript Code

After searching quite long to find a way to call a C# Method from any JavaScript Code which is hostet in a WinForms Webbrowser Control, I found this one here:

C# Code
[ComVisible(true)]
public class ScriptManager
{
Form mForm;

public ScriptManager(Form form)
{
mForm= form;
public void MethodToCallFromScript()
{
mForm.DoSomething()
}
}
}

public class MyForm : Form
{
private Webbrowser mBrowserCtrl;

public MyForm()

{

mBrowserCtrl = new Webbrowser();
mBrowserCtrl.ObjectForScripting= new ScriptManager(this);

}

public void DoSomething(){}

}

JavaScript Code
function HandleSomething()
{
window.external.MethodToCallFromScript();
}


Trackbacks & Pingbacks

Comments

  1. * kashif says:

    Does this window.external.MethodToCallFromScript() works similarly if created in VB.NET? Thank you, even this code is very helpful

    | Reply Posted 16 years, 3 months ago
  2. * globetech says:

    Yes it should work, since this is part of the .net Framework, but it won’t work in .net Compact Framework.

    | Reply Posted 16 years, 3 months ago
  3. * kashif says:

    thanks for the prompt reply. how would i make a vb.net class com visible? thanks again.

    | Reply Posted 16 years, 3 months ago
  4. * braintech says:


    Imports System.Runtime.InteropServices

    <ComVisible(False)> _
    Class SampleClass
    End Class

    for more details have a look at: http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute(VS.71).aspx

    | Reply Posted 16 years, 3 months ago


Leave a comment