Calling C# function within Browser Control
-
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
-
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
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
-
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
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