Click here to Skip to main content
13,355,018 members (58,235 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 23 Nov 2010

Call a C# Method From JavaScript Hosted in a WebBrowser

, 6 May 2011
Rate this:
Please Sign up or sign in to vote.
This demonstrates how you can call a C# method in a Windows Forms application from JavaScript that is hosted in a webpage inside a WebBrowser control on your form.
This sample demonstrates how to call C# from JavaScript. It also shows that parameters can be passed to C# methods.

First, create a Windows Forms application. Then, add a WebBrowser control to your form. Then modify the code for the form so it looks like this:

namespace WindowsFormsApplication6
    // This first namespace is required for the ComVisible attribute used on the ScriptManager class.
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    // This is your form.
    public partial class Form1 : Form
        // This nested class must be ComVisible for the JavaScript to be able to call it.
        public class ScriptManager
            // Variable to store the form of type Form1.
            private Form1 mForm;

            // Constructor.
            public ScriptManager(Form1 form)
                // Save the form so it can be referenced later.
                mForm = form;

            // This method can be called from JavaScript.
            public void MethodToCallFromScript()
                // Call a method on the form.

            // This method can also be called from JavaScript.
            public void AnotherMethod(string message)

        // This method will be called by the other method (MethodToCallFromScript) that gets called by JavaScript.
        public void DoSomething()
            // Indicate success.
            MessageBox.Show("It worked!");

        // Constructor.
        public Form1()
            // Boilerplate code.

            // Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#.
            webBrowser1.ObjectForScripting = new ScriptManager(this);

            // Create the webpage.
            webBrowser1.DocumentText = @"<html>
	            <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                    <br />
                    <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello');"" />

Note that your application may be part of a namespace other than WindowsFormsApplication6, but the rest of the code should work if you follow the above instructions explicitly. I created this tip/trick because somebody asked me a question and they didn't understand this sample that I sent them to. This tip/trick makes the sample more understandable by fixing the two bugs I spotted, adding the using statements that weren't mentioned, and by heavily commenting the code. Hopefully the rest of you will find this of use as well.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States

  • Managing Your JavaScript Library in ASP.NET (if you work with and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 nice tip. thanks! Pin
WhiteRose161129-Nov-10 3:26
memberWhiteRose161129-Nov-10 3:26 
GeneralReason for my vote of 5 Excellent! Great help for me! Pin
JohnSmithPer24-Nov-10 14:12
memberJohnSmithPer24-Nov-10 14:12 
GeneralHow can i pass a JavaScript variable in C# using a button in... Pin
JohnSmithPer24-Nov-10 13:31
memberJohnSmithPer24-Nov-10 13:31 
GeneralCode is working properly! Thanks for your help aspdotnetdev.... Pin
JohnSmithPer24-Nov-10 12:49
memberJohnSmithPer24-Nov-10 12:49 
GeneralThanks for the heads up, but I'm sure this code is running p... Pin
aspdotnetdev23-Nov-10 19:58
protectoraspdotnetdev23-Nov-10 19:58 
GeneralThis code is not running properly....when it executing webBr... Pin
Palash Biswas23-Nov-10 19:44
memberPalash Biswas23-Nov-10 19:44 
GeneralRe: geaglem informed me of the problem, and I have implemented t... Pin
AspDotNetDev5-May-11 19:02
protectorAspDotNetDev5-May-11 19:02 
SuggestionAUDissa (Regarding Calling C# From JavaScript on Windows Mobile) Pin
AspDotNetDev15-Feb-12 8:33
protectorAspDotNetDev15-Feb-12 8:33 
Please post your questions in Quick Answers and somebody may be able to answer you. Post a reply to this message with a link to your question and I may answer it if nobody else gets to it first.

GeneralRe: AUDissa (Regarding Calling C# From JavaScript on Windows Mobile) Pin
boogac29-Feb-12 12:12
memberboogac29-Feb-12 12:12 
GeneralRe: AUDissa (Regarding Calling C# From JavaScript on Windows Mobile) Pin
AspDotNetDev29-Feb-12 16:16
protectorAspDotNetDev29-Feb-12 16:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 May 2011
Article Copyright 2010 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid