Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:54:01+00:00 2026-05-24T21:54:01+00:00

Consider a scenario where I have a WebBrowser Control in WPF application. A web

  • 0

Consider a scenario where I have a WebBrowser Control in WPF application.
A web page is loaded inside WebBrowser Control. The web page contains a button.
The web page is of ASP.NET application.

I want to capture the button click event of the webpage into WPF Form (which hosts WebBrowser Control). Is there any way to achieve this functionality?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:54:02+00:00Added an answer on May 24, 2026 at 9:54 pm

    Here is code that should do exactly what you want with comments to explain what is going on:

    public partial class MainWindow : Window
    {
    
        /// <summary>
        /// This is a helper class.  It appears that we can't mark the Window as ComVisible
        /// so instead, we'll use this seperate class to be the C# code that gets called.
        /// </summary>
        [ComVisible(true)]
        public class ComVisibleObjectForScripting
        {
            public MainWindow Parent;
    
            public void ButtonClicked()
            {
                //Do whatever you need to do.  For now, we'll just show a message box
                Parent.MessageBox.Show("Button was clicked in web page");
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            //Pass an instance of our helper class as the target object for scripting
            webBrowser1.ObjectForScripting = new ComVisibleObjectForScripting{ Parent = this };
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Navigate to your page somehow
            webBrowser1.Navigate("http://www.somewhere.com/");
        }
    
        private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
        {
            //Once the document is loaded, we need to inject some custom JavaScript.
    
            //Here is the JavaScript
            var javascript = @"
    //This is the JavaScript method that will forward the click to the WPF app
    function htmlButtonClicked()
    {
        //Do any other procession...here we just always call to the WPF app
        window.external.ButtonClicked();
    }
    
    //Find the button that you want to watch for clicks 
    var searchButton = document.getElementById('theButton');
    
    //Attach an onclick handler that executes our function
    searchButton.attachEvent('onclick',htmlButtonClicked);
    ";
    
            //Grab the current document and cast it to a type we can use
            //NOTE: This interface is defined in the MSHTML COM Component
            //       You need to add a Reference to it in the Add References window
            var doc = (IHTMLDocument2)webBrowser1.Document;
            
            //Once we have the document, execute our JavaScript in it
            doc.parentWindow.execScript(javascript);
        }
    }
    

    Some of this was taken from http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the scenario where: an ASP.NET webform page has a DropDownList and a ListView.
consider this scenario: I have loaded a Parent entity through hibernate Parent contains a
Consider this scenario: <asp:CheckBoxList> in a master page. the goal is to have all
Consider the scenario of upgrading an ASP.NET MVC project. This question is around up-converting
Consider this scenario; suppose I have WPF window which have four objects bonded to
Consider this scenario: I have two forms (Form1 and Form2). When a button is
Lets consider next scenario: assume I have a web app, and authentication of users
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target
This is a question for a WSS/SharePoint guru. Consider this scenario: I have an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.