. 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 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

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.