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 Cs TerminalsCode:
/*
*
* 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
{< br> [system. runtime. interopservices. comvisibleattribute (true)]
Public partial class mainform: Form
{< br> Public mainform ()
{< br> 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 web browser control opens the page by default.
webcontainer. url = urisale;
}
///
// menu click event
///
///
//
private void jseventtoolstripmenuitem_click (Object sender, eventargs e)
{< br> 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. webform page. The source code is very simple. You can copy it directly.Source codeTest it locally. The source code of the HTML page is as follows:
code [http://www.xueit.com]
test JS event
function fn_call () {
window. external. javascriptcall ("BS sent messages to winform succeeded ");
}< br> -->
the winform and webform instances in net communicate with each other. -www.xueit.com)
well! By now, all operations have been done, which is very simple. If you are interested in testing the code above