. NET winform and webform communicate with each other. CS calls the JS function on the BS page.

Source: Internet
Author: User

Original: In. NET, winform and webform communicate with each other. CS calls the JS function on the BS page.
Time: 21:09:32 Source: Author: dodo
-
-
Now C # is used by more and more friends.. 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:

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 );
}
}
}

-2nd. For webform pages, the source code 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 www.xueit.com welcome ");
}

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>

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.