The method of C # WinForm program interacting with JS

Source: Internet
Author: User
Tags class definition


In the process of developing WinForm programs using C #, we often encounter browser controls embedded with a webbrowser. Many times, we need to control the way the page is displayed in the program, or call a certain JS function in the Web page, in turn, it is possible that the Web page also needs to invoke functions in the program to implement certain functions. Now let me explain how to interact with each other.



The program calls the JS script as follows:



JS Script code:



<script language=”javascript”>
function ShopXG(infor)
{
alert(‘Programming portal - www.bianceng.cn’);
return;
}
</script>



C # code calls are as follows:



Using System.Security.Permissions;



Note: The following two lines need to be added before the class definition, otherwise the call fails!



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



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



The function called:



WebBrowser1.Document.InvokeScript ("Shopxg", new string[] {' ssss '});



Example of calling C # functions in JS:



The C # functions are as follows:



public void ShowMessage(string message)
{
MessageBox.Show(“Programming portal - www.bianceng.cn”);
}



JS in the call method:



<script language=”javascript”>
function InvokeFunc()
{
window.external.ShowMessage(‘呵呵’);
}
</script>





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.