How to get the click event of button generated thorugh XSL in .Net Windows application

Anurag Agarwal
Anurag Agarwal used Ask the Experts™
on
I have an .Net windows application, on that in one of the forms i have windows browser control for showing some html data rendered through xsl. currently that data is read only.

Now the requirement is to give a hyperlink on one of the field of html data. Then on click of the hyperlink i have to open aa another form.

I tried to add a button on my xsl by using
<asp:button id="mybutton" runat="server" text="mybutton" width="40"/>

and in namespace i added
xmlns:asp="remove"

but no button appears on Html, when i tried
<input type="button" name="submit" id="submit" runat="server" Text="Submit" />

Button is added on html but i am not able to get any event on its click

Anurag
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can call an routine of the container application (ie, your application) using JavaScript.

First you need to ensure your application is COM visible by going to the Project Properties > Application tab > Assembly Information, then ensuring "Make assembly COM-visible" is checked.

Then take a look at the below example showing how to "capture" the button click.

Wayne
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim html As String = "<html>" & _
                             "<button type=""button"" onclick=""javascript:window.external.ButtonClick"">Click Me!</button>" & _
                             "</html>"
        With WebBrowser1
            .ObjectForScripting = Me
            .DocumentText = html
        End With
 
    End Sub
 
    Public Sub ButtonClick()
        MsgBox("button was clicked")
    End Sub
 
End Class

Open in new window

Anurag AgarwalPython Developer

Author

Commented:
Hi Webtubbs,
Thanks for the reply.
I incorporated your suggestion to my code, The problem i got is that my current form is inheriting from some base forms so i need to make all those projects also "assembly COM-visible" which i don't want.
Can you please suggest some other solution so that i don't need to chnage any thing in my project configuration.
 
Anurag
There is no other way. To be able to handle events of your button clicks from a WebBrowser in your application, you *must* make it com-visible and use the method I posted above.

Wayne
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Anurag AgarwalPython Developer

Author

Commented:
Just for your information, my project is a big enterprise product which is using installsheild for installation. So any effects of making few of projects as COM visible in the whole solution which contain over 100 projects.
Anurag
Then how about making the class COM-visible?
<System.Runtime.InteropServices.ComVisible(True)> _
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim html As String = "<html>" & _
                             "<button type=""button"" onclick=""javascript:window.external.ButtonClick"">Click Me!</button>" & _
                             "</html>"
        With WebBrowser1
            .ObjectForScripting = Me
            .DocumentText = html
        End With
 
    End Sub
 
    Public Sub ButtonClick()
        MsgBox("button was clicked")
    End Sub
 
End Class

Open in new window

Anurag AgarwalPython Developer

Author

Commented:
I tried your suggestion by making my class as COM visible, got an error
A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class 'MyControl'. However since this class does not have an explicit default interface and derives from non COM visible class 'MyParentControl', the QueryInterface call will fail. This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.
 
Anurag
What about setting 'MyParentControl' to COM-visible, as well?
Anurag AgarwalPython Developer

Author

Commented:
Thanks for the help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial