3

我想做的是从 javascript 调用一个 vb.net 函数,
这是我的 html 代码。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>Untitled
        Page
    </title>
    <script
    src="jquery.js"></script>
        <script
        type="text/javascript">
            $(function(){
            $("button").click(showVbHelloWorld)
            function
            showVbHelloWorld()
            {
            window.external.showVbHelloWorld();
            }
            })

            </script>
</head>

<body>
    <button>A</button>

</body>

</html>

这是我的 vb.net 代码

Imports System
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> 

<System.Runtime.InteropServices.ComVisibleAttribute(True)>

<Microsoft.VisualBasic.ComClass()> 

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles Button1.Click

    Me.WebBrowser1.ObjectForScripting = Me

End Sub

Public Sub showVbHelloWorld()

    MsgBox("Hello")

End Sub

End Class

仍然在按钮上单击我得到错误未捕获的类型错误
:对象#没有方法'showVbHelloWorld'

对不起我的代码格式不均匀......我是stackoverflow的新手......

4

2 回答 2

3

这是一个显示如何操作的链接:Call VB method from JavaScript

它基本上说有两种方式,Ajax 或 Postback。这是回发方法:

.aspx 文件:

<script type="text/javascript">
 <!--
 function callServersideFunction()
 {
  var someValueToPass = 'Hello server';

 __doPostBack('CustomPostBack', someValueToPass);
 }
 // -->
 </script>

aspx.vb 文件:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  ' Insure that the __doPostBack() JavaScript method is created...
  Me.ClientScript.GetPostBackEventReference(Me, String.Empty)


 If Me.IsPostBack Then
   Dim eventTarget As String
   Dim eventArgument As String

  If ( (Me.Request("__EVENTTARGET") Is Nothing)
    eventTarget = String.Empty
   Else
    eventTarget = Me.Request("__EVENTTARGET"))
   If ( (Me.Request("__EVENTARGUMENT") Is Nothing)
    eventArgument = String.Empty
   Else
    eventArgument = Me.Request("__EVENTARGUMENT"))

  If eventTarget = "CustomPostBack" Then
    Dim valuePassed As String = eventArgument
    ' Call your VB method here...
   End If
  End If
 End Sub
于 2013-10-01T14:37:49.870 回答
1

你必须把这个声明

Me.WebBrowser1.ObjectForScripting = Me

里面form_load

Private Sub form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Me.WebBrowser1.ObjectForScripting = Me
End Sub

看到这个网站:- http://www.codeproject.com/Articles/35373/VB-NET-C-and-JavaScript-communication

于 2016-03-09T11:35:47.243 回答