Mutual debugging of winform controls webbrowser and JS scripts

Source: Internet
Author: User
Mutual debugging of winform controls webbrowser and JS scripts

Default category
2007-09-06 22:40:26
Read257

Comment0


Font Size:Large
Medium
Small

1) Call JS functions in C #

Modify the attributes of webbrowser so that C # can call the js method:

Webbrowser1.objectforscripting = this;

If you want to pass a value, you can define the object [] array.

The specific method is as follows:

First, define the method called by C # In JS:

Function messageaa (Message)

{

Alert (Message );

}

Call the js method messageaa in C #

Private void button#click (Object sender, eventargs E)
{
// Call the MessageBox method of JavaScript and input parameters

Object [] objects = new object [1];

Objects [0] = "C # Diao JavaScript ";

Webbrowser1.document. invokescript ("messageaa", objects );
}

2) Call the C # method in JS

Calling the C # method in JS is relatively simple:

First, define the method called by JS in C:

Public void mymessagebox (string message)
{
MessageBox. Show (Message );
}

Call the C # method in JS:

<! -- Call C # Method -->
<Button onclick = "window. External. mymessagebox ('javascript access C #Code') ">
Javascript access C # Code </button>

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.