C # how to interact with JS after the form program is embedded in webbrowser

Source: Internet
Author: User
Developing winform using C # Program During the process, we often encounter browser controls embedded with a webbrowser. In many cases, we need to control the webpage display mode in the program, or call a JS function in the webpage. In turn, it is also possible that the webpage also needs to call functions in the program to implement some functions. Next I will explain how to interact with each other.
The JS script called by the program is as follows:
JS script Code :

<Script language = "JavaScript">
Function shopxg (INFOR)
{
Alert ('I want to open an online shop and buy an independent online shop system with the most complete template and the most professional system! ');
Return;
}
</SCRIPT>

C # The code is called as follows:
UsingSystem. Security. permissions;

Note: The following two lines must be added before the class definition; otherwise, the call fails!
[Permissionset (securityaction. Demand, name ="Fulltrust")]
[System. runtime. interopservices. comvisibleattribute (True)]

Called functions:
Webbrowser1.document. invokescript ("Shopxg",New String[] {'Sss '});

Example of Calling C # functions in JS:
C # functions:
Public VoidShowmessage (StringMessage)
{
MessageBox. Show ("open an online shop for free and invite you to join the independent online shop system with purchase !");
}

Method of calling in JS:
<Script language = "JavaScript">
Function invokefunc ()
{
Window. External. showmessage ('H ');
}
</SCRIPT>

When developing a winform program using C #, we often encounter browser controls embedded with a webbrowser. In many cases, we need to control the webpage display mode in the program, or call a JS function in the webpage. In turn, it is also possible that the webpage also needs to call functions in the program to implement some functions. Next I will explain how to interact with each other.
The JS script called by the program is as follows:
JS script code:
<Script language = "JavaScript">
Function shopxg (INFOR)
{
Alert ('I want to open an online shop and buy an independent online shop system with the most complete template and the most professional system! ');
Return;
}
</SCRIPT>
C # The code is called as follows:

UsingSystem. Security. permissions;

Note: The following two lines must be added before the class definition; otherwise, the call fails!
[Permissionset (securityaction. Demand, name ="Fulltrust")]
[System. runtime. interopservices. comvisibleattribute (True)]

Called functions:
Webbrowser1.document. invokescript ("Shopxg",New String[] {'Sss '});

example of Calling C # function in JS:
C # function:
Public void showmessage ( string message)
{< br> MessageBox. show! ");
}< br> call method in JS:

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.