Everyone knows.. Net can be used to develop winform and webform pages. Sometimes BS + CS must be used in the development project process. In this case, the website will create an instance to test the instances in which winform and webform communicate with each other, let's take a look at the effect first:
Winform calls JS functions on the BS page
Send information to winform on the webform page
Well, after reading the above results, let's take a look at how to implement it.
1. Open vs2008 to create a winform project, drag the browser control in mainform, and run the following command: webcontainer
Below are all the CS code:
/*
*
* Name: CS and BS communicate with each other
* Author: CC
* Official: http://www.cnblogs.com/chjun2000/
*/
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Namespace testjswin
{
[System. runtime. interopservices. comvisibleattribute (true)]
Public partial class mainform: Form
{
Public mainform ()
{
Initializecomponent ();
This. webcontainer. objectforscripting = This; // This sentence is critical. It mainly interacts with JS on the page.
Uri urisale = new system. Uri ("http: // localhost: 8012/index.htm"); // the browser control opens the page by default.
Webcontainer. url = urisale;
}
/// <Summary>
/// Menu Click Event
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void jseventtoolstripmenuitem_click (Object sender, eventargs E)
{
Webcontainer. navigate ("javascript: fn_test (); void (0 );");
}
/// <Summary>
/// Bs call Method
/// </Summary>
/// <Param name = "strshow"> </param>
Public void javascriptcall (string strshow)
{
MessageBox. Show (strshow );
}
}
}
Well, after finishing winform, the following is the practice of http: // localhost: 8012/index.htm page.
2nd. the source code of the webform page is very simple. You can directly copy the source code to a local test. The source code of the HTML page is as follows:
Code [http://www.xueit.com]
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> test JS event </title>
<Script language = "JavaScript" type = "text/JavaScript">
<! --
Function fn_test (){
Alert ("Hello, CS succeeded in calling js- it http://www.xueit.com/ ");
}
Function fn_call (){
Window. External. javascriptcall ("BS successfully sent messages to winform ");
}
-->
</SCRIPT>
</Head>
<Body>
Net winform and webform communication instances -www.xueit.com)
<Input type = "button" value = "Call winform methed" onclick = "fn_call ()"/>
</Body>
</Html>
Well! So far, all operations have been done, which is very simple. If you are interested in testing the code above