1

I have a javascript file and I have a method "Test" in that method I like to call a c# function.

The c# function is not in the same file as in the javascript file.

It's in a .cs file. So how can I manage that the javascript functions is able to call the c# function ?

I already searched on the internet but most solutions are based on a aspx and apx.cs file...

I tried something like this:

viewer.js

function Test() {
alert("Hello world-2");
window.external.MethodToCallFromScript();
}

ScriptManager.cs

[ComVisible(true)]
    public class ScriptManager
    {
        public void MethodToCallFromScript()
        {
            Debug.WriteLine("test");
        }
    }

But it did not work...

Can somebody help me?

Thanks!

3
  • are you using asp.net? Commented Apr 3, 2013 at 10:51
  • 1
    what are you trying to do? Commented Apr 3, 2013 at 10:56
  • I want to have two seperate files like I now have: a .cs file where my function is (at this moment just a messagebox showing "hello world" but later it's more than only that-> HTTPWebrequest) and a .js file where I can call my c# function... Commented Apr 3, 2013 at 12:35

1 Answer 1

1

In order to have this working, you must set the ObjectForScripting-property of the WebBrwoser-control.

Here is an example

using System;
using System.Windows.Forms;
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
    private WebBrowser webBrowser1 = new WebBrowser();
    private Button button1 = new Button();

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    public Form1()
    {
        button1.Text = "call script code from client code";
        button1.Dock = DockStyle.Top;
        button1.Click += new EventHandler(button1_Click);
        webBrowser1.Dock = DockStyle.Fill;
        Controls.Add(webBrowser1);
        Controls.Add(button1);
        Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.AllowWebBrowserDrop = false;
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.WebBrowserShortcutsEnabled = false;
        webBrowser1.ObjectForScripting = this;
        // Uncomment the following line when you are finished debugging. 
        //webBrowser1.ScriptErrorsSuppressed = true;

        webBrowser1.DocumentText =
            "<html><head><script>" +
            "function test(message) { alert(message); }" +
            "</script></head><body><button " +
            "onclick=\"window.external.Test('called from script code')\">" +
            "call client code from script code</button>" +
            "</body></html>";
    }

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

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("test",
            new String[] { "called from client code" });
    }

}

And here is the link.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution but I actually I dont want the HTML code inside my c# code it's need to be two seperate files...
The significant line in the example is this one: webBrowser1.ObjectForScripting = this;; define yourself a class, create an object and pass it to ObjectForScripting. Also, see the link I provided for clarification

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.