Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling C# function within Browser Control

Calling C# function within Browser Control

Scheduled Pinned Locked Moved C#
questioncsharphtml
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adnan Siddiqi
    wrote on last edited by
    #1

    Hi maybe i am not making any sense..i am creating a HTML file on runtime and opening it in a Webbrowser control,the html also have links..instead of assigning html or other http file link in SRC tag,i want to call my c# function which grab the particular text from webbrowser control and pass it in some C# function,for instance,my HTML have links 1)Apple 2)Oranges,when i click Apple,then it calls MessageBox with text "Apple" etc..is it possible?how can I inject some code to call c# based routines Thanks

    T 1 Reply Last reply
    0
    • A Adnan Siddiqi

      Hi maybe i am not making any sense..i am creating a HTML file on runtime and opening it in a Webbrowser control,the html also have links..instead of assigning html or other http file link in SRC tag,i want to call my c# function which grab the particular text from webbrowser control and pass it in some C# function,for instance,my HTML have links 1)Apple 2)Oranges,when i click Apple,then it calls MessageBox with text "Apple" etc..is it possible?how can I inject some code to call c# based routines Thanks

      T Offline
      T Offline
      techieboi
      wrote on last edited by
      #2

      I can think of two possible solutions to your problem. The first is to hook-up an event handler for the browser control's BeforeNavigate2 event. You can then use this to parse special commands passed in the SRC attribute and execute particular methods. An example is shown below. HTML: Calls Host Form's MyCommand Method C#: private void WebBrowser_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e) { // Determine if this is a proper url or a command if (((string)e.uRL).IndexOf("command:") != -1) { // Call the appropriate function for the command switch (((string)e.uRL).Replace("command:", "")) { case "mycommand": this.MyCommand(); break; } // Stop the browser from performing the navigation e.cancel = true; } } The other method is to hook up some event sinks to capture the DHTML events. Hope this helps. Aaron

      A 1 Reply Last reply
      0
      • T techieboi

        I can think of two possible solutions to your problem. The first is to hook-up an event handler for the browser control's BeforeNavigate2 event. You can then use this to parse special commands passed in the SRC attribute and execute particular methods. An example is shown below. HTML: Calls Host Form's MyCommand Method C#: private void WebBrowser_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e) { // Determine if this is a proper url or a command if (((string)e.uRL).IndexOf("command:") != -1) { // Call the appropriate function for the command switch (((string)e.uRL).Replace("command:", "")) { case "mycommand": this.MyCommand(); break; } // Stop the browser from performing the navigation e.cancel = true; } } The other method is to hook up some event sinks to capture the DHTML events. Hope this helps. Aaron

        A Offline
        A Offline
        Adnan Siddiqi
        wrote on last edited by
        #3

        Thanks i did find some links on this site 1) http://www.codeproject.com/csharp/winformiehost.asp[^] and other is http://www.codeproject.com/books/0764549146_8.asp?df=100&forumid=13574&exp=0&select=920127[^] i also found some links to call javascript inside C# i am going thru all these possibilities -adnan

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups