gpt4 book ai didi

javascript - 从 javascript 调用 vb.net 函数

转载 作者:行者123 更新时间:2023-11-29 10:18:11 24 4
gpt4 key购买 nike

我想做的是从 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 的新手...

最佳答案

这是一个显示如何操作的链接: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

关于javascript - 从 javascript 调用 vb.net 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17122683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com