This article is about embedding in the. NET in the HTML file shown in the WebBrowser control, how to invoke the C # code. It is now very common to embed Web pages in a client program, such as Tencent's News pop-up box.
. NET WebBrowser the kernel should still be ie. In IE, the Window object has a external property that provides an external interface. Can execute the host code. To invoke the C # method through this property, its host object must be ComVisible. For example, we place a WebBrowser control on a form and prepare a method OpenForm for JS invocation.
Copy Code code as follows:
[System.Runtime.InteropServices.ComVisibleAttribute (True)]
public partial class Main:KSBiz.UI.KSForm
{
Public Main ()
{
InitializeComponent ();
}
The rest slightly
public void OpenForm (string s)
{
Basicinfo.customerlist f = new StockManage.BasicInfo.CustomerList ();
F.show ();
F.mdiparent = this;
Webbrowser1.visible = false;
}
}
Prepare an HTML file again:
Copy Code code as follows:
<body>
<a href= ' # ' onclick= ' window.external.OpenForm ("test"); ' > Customer list </a>
</body>
In an HTML file, you can invoke the C # method by using the method above.
In the Formload event, set the URL for the WebBrowser control, and set the host for the script execution:
Copy Code code as follows:
private void Main_load (object sender, EventArgs e)
{
System.IO.FileInfo file = new System.IO.FileInfo ("top.htm");
The page path displayed by the WebBrowser control
Webbrowser1.url = new Uri (file. FullName);
Sets the current class to be accessible by script
Webbrowser1.objectforscripting = this;
}
That's good.