how to use WebBrowser control in C#.Net

 

Generally in windows applications (or win forms) we use server side pages (like Classic ASP, ASP.Net) as back end to connect to database. Win form application run in client system and call server by using Classic ASP or ASP.Net pages. We can call server side pages by using HTTP Web request or through windows webBrowser control.

 

In HTTP Web request, we just create the object for HttpWebRequest and we will provide URL of the server page. Through HttpWebResponse object we will get the response. This is the good approach when we call the server pages just to connect to database.

 

                                                                                                                                                                  

 

But sometimes, we need to call windows application methods on server. This is possible by using javascript.  In this case our first approach will not work. We use WebBrowser windows control in this scenario.

 

Here we will discuss about how to use WebBrowser Control in Win forms. WebBrowser Control displays the server side web page on windows application. So we can use any functionality like normal web application.

 

Open Visual studio Framework->New Project ->Select Windows application ->give name as WebBrowserControl.

 

Drag and Drop WebBrowser control from Toolbox.  Create new Asp.Net page and add some javascript function, static content to display on win form as shown below.              

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head runat="server">

 

    <title>ASP.Net Web Page</title>

 

    <script language="javascript">

 

        function SayHello()

 

        {

 

            window.external.SayHi(); //SayHi() is the C# method on win form application

 

        }

 

    </script>

 

</head>

 

<body>

 

    <form id="form1" runat="server">

 

    <div>

 

        <input type="button" onclick="SayHello();" />

 

    </div>

 

    </form>

 

</body>

 

</html>

 

Host this webpage on server and identify the URL.

 

Here we have javascript function which calls winform application method  SayHi().

 

Add the System.Security.Permissions namespace to win form  which allows you to call C# methods from javascript and below code.

 

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]

 

    [System.Runtime.InteropServices.ComVisibleAttribute(true)]

 

    public partial class Form1 : Form

 

    {

 

        public Form1()

 

        {

 

            InitializeComponent();

 

        }

 

 

 

        public void SayHi()

 

        {

 

            MessageBox.Show("Hello...From C#");           

 

        }

 

 

 

        private void Form1_Load(object sender, EventArgs e)

 

        {

 

            webBrowser1.Navigate(new Uri("http://localhost/web1/ASPNet.aspx"));

 

            webBrowser1.ObjectForScripting = this;

 

        }   

 

    }

 

Here we have SayHi() C# method which calls from javascript. Provide the correct URL for webBrowser1 control to navigate to the required web page.