Haz
Haz

Reputation: 35

How to Pass data from C# server side to htm through webBrowser control?

I am facing a problem passing string to HTML page through javascript.

I have a window form, A HTML file, where I have my Javascript and HTML code.

In the function in C# page, I have a string that I need to send to the HTML page through javascript. But I can not pass it. Please advise me. Thanks

My C# method code below

    private void Form1_Load(object sender, EventArgs e)
        {
        Assembly assembly = Assembly.GetExecutingAssembly();
        StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("ProjectName.Maps.html"));
        webBrowser1.DocumentText = reader.ReadToEnd();

        ***//pass getDefaultMap() value (str) to the javascript in Maps.html page.***
        }
    private string getDefaultMap()
        {
        string str;
        str = (@"Exec SP_Map_Display @Opt=1");            
        return str ;
        }

My HTML page is below

<body>
<script>
    $(document).ready(function () {
        $("#btnSubmit").click(function () {    
     ***// Get the data from C# code str***
    }
</script>    

<input type="button" name="btnSubmit" value="Submit" />
<div id="dvMap">
</div>
</body>

Upvotes: 2

Views: 481

Answers (1)

kshkarin
kshkarin

Reputation: 584

Assuming this is WinForms since there's a WebBrowser control, to call C# code from the HTML page JavaScript can be accomplished with this minimum example:

  1. Simple HTML page added to the root of the project and Properties was setup to Copy to Output Directory: Copy if newer this will ensure there's a simple page for testing:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>WebForms WebBrowser Control Client</title>
</head>
<body>
    <input type="button" onclick="getLocations()" value="Call C#" />
    <script type="text/javascript">
        function getLocations() {
            var locations = window.external.SendLocations();
            alert(locations);
        }
    </script>
</body>
</html>
  1. The JS function getLocations will call C# method SendLocations, the important parts are the Form1 class annotations and setting webBrowser1.ObjectForScripting = this :

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

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.ObjectForScripting = this;

        var path = Path.GetFullPath("Client.html");
        var uri = new Uri(path);
        webBrowser1.Navigate(uri);
    }

    public string SendLocations()
    {
        return "SF, LA, NY";
    }
}
  1. Clicking the HTML button Call C# will show a popup with the return value from C# method

enter image description here

Upvotes: 1

Related Questions