JavaScript calls C # code _javascript tips

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.