C # interaction between the webbrowser control and JS

Source: Internet
Author: User

Knowledge:

[C-sharp]
View plaincopyprint?
  1. [System. runtime. interopservices. comvisibleattribute (True)]
  2. This is to set this class to com accessible
  3. URL property: the webpage path displayed by the webbrowser Control
  4. Objectforscripting property: This object can be accessed by the script code contained in the webpage displayed in the webbrowser control.
  5. Javascript calls the C # public method through window. External. This is the public method contained in the instance of the class set by the objectforscripting attribute.
  6. // Web page path displayed by the webbrowser Control
  7. Webbrowser1.navigate (NewUri (system. environment. currentdirectory + @ "/xxx.html ",
    Urikind. relativeorabsolute ));
  8. // Set the current class to be accessed by a script
  9. Webbrowser1.objectforscripting =This;

[System. runtime. interopservices. comvisibleattribute (true)] <br/> This is to set this class to com accessible </P> <p> URL attribute: the webpage path displayed by the webbrowser Control </P> <p> objectforscripting attribute: This object can be accessed by the script code contained in the webpage displayed in the webbrowser control. <br/> JavaScript uses window. external calls the C # public method. This is the public method contained in the instance of the class set by the objectforscripting attribute. </P> <p> // web page path displayed by the webbrowser Control <br/> webbrowser1.navigate (New uri (system. environment. currentdirectory + @ "/xxx.html", urikind. relativeorabsolute); <br/> // set the current class to be accessed by a script <br/> webbrowser1.objectforscripting = This; <br/>

. CS

[C-sharp]
View plaincopyprint?
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. LINQ;
  4. UsingSystem. text;
  5. UsingSystem. windows;
  6. UsingSystem. Windows. controls;
  7. UsingSystem. Windows. Data;
  8. UsingSystem. Windows. documents;
  9. UsingSystem. Windows. input;
  10. UsingSystem. Windows. Media;
  11. UsingSystem. Windows. Media. imaging;
  12. UsingSystem. Windows. Navigation;
  13. UsingSystem. Windows. shapes;
  14. UsingSystem. Web;
  15. UsingSystem. Security. permissions;
  16. NamespaceWpfapplication1
  17. {
  18. /// <Summary>
  19. /// Interaction logic for window1.xaml
  20. /// </Summary>
  21. PublicPartial
    ClassWindow1: Window
  22. {
  23. PublicWindow1 ()
  24. {
  25. Initializecomponent ();
  26. Basic DS =NewBasic ();
  27. Webbrowser1.navigate (NewUri (system. environment. currentdirectory + @ "/aaa.html ",
    Urikind. relativeorabsolute); // obtain the calendar file in the root directory
  28. Webbrowser1.objectforscripting = Ds; // This object can be accessed by the script code contained in the webpage displayed in the webbrowser control.
  29. }
  30. Private
    VoidButton_click (ObjectSender, routedeventargs E)
  31. {
  32. Textbox1.text = dosomething. Name;
  33. }
  34. }
  35. [System. runtime. interopservices. comvisibleattribute (True)] // Set this class to com accessible
  36. Public
    ClassBasic
  37. {
  38. Public
    Static
    StringName;
  39. Public
    StringName
  40. {
  41. Get{
    ReturnName ;}
  42. Set{Name = value ;}
  43. }
  44. Public
    VoidClickevent (StringStr)
  45. {
  46. This. Name = STR;
  47. }
  48. }
  49. }

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. data; <br/> using system. windows. documents; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. imaging; <br/> using system. windows. navigation; <br/> using system. windows. shapes; <br/> using system. web; <br/> using system. security. permissions; <br/> namespace wpfapplication1 <br/> {<br/> /// <summary> <br/> // interaction logic for window1.xaml <br/> // </Summary> <br/> Public partial class window1: window <br/>{< br/> Public window1 () <br/>{< br/> initializecomponent (); <br/> basic DS = new basic (); <br/> webbrowser1.navigate (New uri (system. environment. currentdirectory + @ "/aaa.html", urikind. relativeorabsolute); // obtain the calendar file of the root directory <br/> webbrowser1.objectforscripting = Ds; // This object can be accessed by the script code contained in the webpage displayed in the webbrowser Control <br/>}< br/> private void button_click (Object sender, routedeventargs E) <br/>{< br/> textbox1.text = dosomething. name; <br/>}</P> <p >}< br/> [system. runtime. interopservices. comvisibleattribute (true)] // set this class to com accessible <br/> public class basic <br/>{< br/> Public static string name; <br/> Public string name <br/>{< br/> get {return name ;}< br/> set {name = value ;} <br/>}< br/> Public void clickevent (string Str) <br/>{< br/> This. name = STR; <br/>}< br/>

Html

[C-sharp]
View plaincopyprint?
  1. <HTML>
  2. <Head>
  3. <MCE: script language = "JavaScript" type = "text/JavaScript"> <! --
  4. Function selec ()
  5. {
  6. VaR divv = Document. getelementbyid ('div2'). innertext;
  7. Window. External. clickevent (divv );
  8. }
  9. // --> </MCE: SCRIPT>
  10. </Head>
  11. <Body>
  12. <Form>
  13. <Div id = "div1" onclick = "selec ();"> 000000000000 </div>
  14. <Div id = "div2"> 111111 </div>
  15. </Form>
  16. </Body>
  17. </Html>

<HTML> <br/> <pead> <br/> <MCE: script language = "JavaScript" type = "text/JavaScript"> <! -- <Br/> function selec () <br/> {<br/> var divv = document. getelementbyid ('div2 '). innertext; <br/> window. external. clickevent (divv); <br/>}< br/> // --> </MCE: SCRIPT> <br/> </pead> <br/> <body> <br/> <form> <br/> <Div id = "div1" onclick = "selec (); "> 000000000000 </div> <br/> <Div id =" div2 "> 111111 </div> <br/> </form> <br/> </body> <br/> </ptml>

If you need to click the button during running and then pass the value to the page for display, use the following method to pass the value

This. Webbrowser1.invokescript ("functions in JS", "values to be passed ");

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.