Original: In. net, winform and webform communicate with each other. CS calls the JS function on the BS page.

Source: Internet
Author: User

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

 

 

 

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.