0

I am trying to post the changes made to an HTML document in the browser control, to a VB.net form. But I am unable to access the Vb.net functions from within the javascript. I am unsure what I am missing here.

This is the vb.net function I am trying to call from javascript.

   Public Sub receiveChangesFromJS(ByVal changes As String)
        htmlChanges = changes

        updateXML()
    End Sub

The HTML page is loaded in "Browser1" browser control, in the same vb.net form. Clearly, I need to add some reference somewhere. But, I can't find how or where to add said reference.

3
  • You can call JavaScript functions using VB.NET, but I don't recall being able to run a VB.NET function using JavaScript directly. Of course, you can do it indirectly, send the function name and args as a URL back to your browser control after the ?... ex: form.html?myFunc=arg1 then access it using VB.NET using .locationurl and parse it and run it. Are you using the WebBrowser Control? If so, add the tag! Commented Aug 21, 2014 at 8:02
  • Is it formbase vb.net or do you mean asp.net? Possible duplicate: stackoverflow.com/questions/17122683/… Commented Aug 21, 2014 at 8:16
  • It is a vb.net form in an existing desktop application. So, I need to add some way for user to edit the HTML document in vb.net's control. I couldn't think of anything better than using javascript. And now, I can get the user to edit the HTML, but can't post it back. :( Commented Aug 21, 2014 at 8:40

1 Answer 1

0

If your concern is to let the user edit the html document loaded in the WebBrowser control, then here is an easy solution.

You can customize this, but for this example, add a TextBox (multiline), three Buttons and a WebBrowser control to a Form and the following code:

'' This is to let us work with objects easily, without the need of lots of CType and DirectCast 
Option Strict Off

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '' replace with your url 
        WebBrowser1.Navigate("http://www.google.com")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '' you get the HTML of the webbrowser back
        TextBox1.Text = WebBrowser1.DocumentText
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '' this is just a generic way for example only. 
        '' You may want to give option buttons instead of asking user to type tag name.
        Dim elementTag As String = InputBox("Enter the TAG for HTML element you want to add:", "Add Element")
        If Not String.IsNullOrEmpty(elementTag) Then
            Dim childControl = WebBrowser1.Document.CreateElement(elementTag)
            childControl.InnerHtml = "Your " & elementTag & " control..."
            WebBrowser1.Document.Body.InsertAdjacentElement(HtmlElementInsertionOrientation.AfterBegin, childControl)
            childControl.Focus()
        End If
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ' To turn On the edit mode.
        Dim axObj As Object
        axObj = WebBrowser1.ActiveXInstance
        axObj.document.designmode = "On"
    End Sub
End Class

Now clicking the Button1 will load your document in the WebBrowser control for editing. Clicking Button2 will get you the HTML of current (edited) document in the WebBrowser. Clicking Button3 will help you add new elements to the HTML document.

Hope this helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

I already have this solution in place. Since, users have to add/delete elements, it is difficult to explain them how it all works. They don't have any understanding of html elements. That's why we are creating new method, where clicking html page will allow them to edit it right there. I have that part done by creating text area on click position. Now data transfer to vb.net is only thing pending.
WebBrowser.DocumentText gives you the HTML source of whatever is currently loaded in the WebBrowser. And you already have the original source. So you can easily compare the changes, isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.