In winForm, how does one implement the Webbrowser control to load html pages ?, Winformwebbrowser

Source: Internet
Author: User

In winForm, how does one implement the Webbrowser control to load html pages ?, Winformwebbrowser

The Webbrowser control is very easy to load html pages. Drag a webbrowser control to the form, and load webpage resources by using the code: webBrowser1.Navigate ("cityApi.html.

To implement winform interaction with Web pages, you need to use the webbrowserappsdocumentcompleted event (that is, InterAction after html documents are loaded ):

The winform background can use the statement webBrowser1.Document. invokeScript ("loadInfo", new object [] {parameter 1 ,..., parameter m}); call the javascript function loadInfo to upload data to the webpage;
Javascript Functions can use the following statement: window. external. ShowMsg (str); call the winform background function ShowMsg (string str );

The following code shows a small example:
[FrmCityApi. cs]
Private void FrmCityApi_Load (object sender, EventArgs e) {webBrowser1.DocumentCompleted + = new System. windows. forms. webBrowserDocumentCompletedEventHandler (this. webBrowser1_DocumentCompleted); // initialize webbrowser to ensure communication between js and C #. webBrowser1.ObjectForScripting = this; // load webbrowser webpage resource webBrowser1.Navigate ("cityApi.html ");} /// <summary> /// load the air index information after the webbrowser file is loaded /// </summary> /// <param name = "sende R "> </param> // <param name =" e "> </param> private void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {try {if (webBrowser1.Document! = Null) {// load the air index information webBrowser1.Document. invokeScript ("loadInfo", new object [] {city_name, api_date.ToString (), api. toString (), sitenum. toString ()}) ;}} catch (Exception) {DialogHelper. dlgWarn (this, "loading air index information error ");}}

【CityApi.html]
<Script type = "text/javascript">/* function Run (str) {window. external. showMsg (str);} */function loadInfo (cityName, apiDate, apidata, num) {document. getElementById ("city_name "). innerHTML = cityName; document. getElementById ("api_time "). innerHTML = "Data Update time:" + apiDate; document. getElementById ("city_api_data "). innerHTML = apidata; document. getElementById ("sites_count "). innerHTML = num + "monitoring sites" ;}</script>

End !!!

Related Article

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.