2

I loaded a html local file in a WebBrowser control within a WinForm. In the html code, I defined some variables in %variables%.

My question is how to transfer/reference strings/data from WinForm to the %viarables% defined in the loaded html page, and refresh the loaded html page displayed on the WebBrowser.

Following is the code of loading a local html file from. Any suggestion will be appreciated.

By the way, it is a WinForm application, not asp.Net.

        string curDir = Directory.GetCurrentDirectory();
        this.webBrowser1.Url = new Uri(String.Format("file:///{0}/{1}", curDir + "\\Forms", fileName));
John Wang
  • 59
  • 8

2 Answers2

1

Instead of trying to replace your variables after loading the HTML, which could be problematic, why not read the source file, replace the variables, then load the updated document into the web browser control? This seems much more straightforward.

For example:

string dir = Path.Combine(Directory.GetCurrentDirectory(), "forms");
string html = File.ReadAllText(Path.Combine(dir, fileName));
foreach (string variable in GetListOfVariables())
{
    html = html.Replace(variable, GetReplacementForVariable(variable));
}
webBrowser1.DocumentText = html;
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
0

Do you have control over contents of the HTML file? You could try InvokeScript method paired with a Javascript function prepared in the document beforehand.

If you don't control the contents, then you can inject javascript from your code. Here are some examples of doing that.

Community
  • 1
  • 1
Dmitriy
  • 1,852
  • 4
  • 15
  • 33