从javascript访问vb.net表单控件

时间:2014-08-21 07:56:12

标签: javascript html vb.net

我试图将对浏览器控件中的HTML文档所做的更改发布到VB.net表单中。但是我无法从javascript中访问Vb.net函数。我不确定我在这里缺少什么。

这是我试图通过javascript调用的vb.net函数。

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

        updateXML()
    End Sub

HTML页面以“Browser1”浏览器控件加载,采用相同的vb.net格式。显然,我需要在某处添加一些参考。但是,我找不到如何或在哪里添加所述参考。

1 个答案:

答案 0 :(得分:0)

如果您的问题是让用户编辑WebBrowser控件中加载的html文档,那么这是一个简单的解决方案。

您可以自定义此选项,但是对于此示例,将一个TextBox(多行),三个Buttons和一个WebBrowser控件添加到Form和以下代码:

'' 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

现在单击Button1将在WebBrowser控件中加载文档以进行编辑。 单击Button2将获取WebBrowser中当前(已编辑)文档的HTML。 单击Button3将帮助您向HTML文档添加新元素。

希望这会有所帮助:)