Access the. NET class through js in WebBrowser to complete user registration-IEBrowser [3]

Source: Internet
Author: User

A few days ago, I published an article about using IEBrowser to access the variables or JSON data on the page. Now I want to explain how js scripts access the. NET class in WebBrowser.

You can use the Scripting attribute of IEBrowser or the ObjectForScripting attribute of WebBrowser to set objects that can be accessed by js scripts. In js scripts, you can use window. external to call object methods.

To demonstrate this, I have already created a local database and the corresponding dataset, and added an InsertStudent Method to the data adapter. These codes are not displayed here, you can refer to the demo link at the end of the article to see the School class code:

[ComVisible (true)] public class School {public string Register (string email, string realName, int age) {if (string. isNullOrEmpty (email) return "EMail cannot be blank"; if (string. isNullOrEmpty (realName) return "name cannot be blank"; if (age <= 6 | age> 100) return "age should be between 7 and 100"; try {SchoolDSTableAdapters. studentTableAdapter adapter = new SchoolDSTableAdapters. studentTableAdapter (); adapter. insertStudent (email, realName, age); return "registration successful";} catch {return "registration failed" ;}} the School class must be modified using the ComVisible attribute, if ComVisible is set to true, School can be accessed by js.

We can see that School has only one simple Register registration method. In this method, we checked the validity of parameters such as email address, name, and age, then, a student information is added to the database through the previously created data adapter, and Register returns a string. After the js script calls the Register method, it can display the string to the operator.

The following code is school.htm on the page, which is the interface on which the operator interacts with the program:

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

In the js function register, we access the Register Method of the School class through window. external, pass the email address, name and age as the parameter, and pop up the returned string.

In fact, writing window. external does not represent the School class. We need to perform the last step.

Finally, in the FormManaged window, write the following code. The window contains a webBrowser control named WebBrowser:

Public partial class FormManaged: Form {private readonly IEBrowser ie; public FormManaged () {InitializeComponent (); this. ie = new IEBrowser (this. webBrowser);} private void FormManaged_Load (object sender, EventArgs e) {this. ie. navigate (Path. combine (AppDomain. currentDomain. baseDirectory, "school.htm"); this. ie. IEFlow. wait (new UrlCondition ("w", "school.htm", StringCompareMode. contain); this. ie. scripting = new School () ;}} in the window, we define the ie browser object as the read-only field of the window class.

When loading a window, we first use Navigate to Navigate WebBrowser to the school.htm page we have compiled. When the page is generated, we need to call it as the output directory, then we will use the Wait method to Wait until the loading of the school.htm page is complete. Finally, we can use the Scripting attribute to set js scripts to access the School class.

When the Scripting attribute is set to a new School instance, it means that window. external in js will represent School.

 

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.