vb.net 从javascript调用vb.net函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17122683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling vb.net function from javascript
提问by Krunal Shah
what I want to do is call a vb.net function from javascript
here is my html code.
我想要做的是从 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>
and here is my vb.net code
这是我的 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
still on button click im geting error
Uncaught TypeError: Object # has no method 'showVbHelloWorld'
仍然在按钮上单击我收到错误
Uncaught TypeError: Object # has no method 'showVbHelloWorld'
sorry for my uneven formatting of the code...i am new to stackoverflow...
对不起,我的代码格式不均匀......我是stackoverflow的新手......
回答by Steve
Here is a link that shows how to do it: Call VB method from JavaScript
这是一个显示如何操作的链接:Call VB method from JavaScript
It basically says there are 2 ways, Ajax or Postback. Here is the postback method:
它基本上说有两种方式,Ajax 或 Postback。这是回发方法:
aspx file:
aspx文件:
<script type="text/javascript">
<!--
function callServersideFunction()
{
var someValueToPass = 'Hello server';
__doPostBack('CustomPostBack', someValueToPass);
}
// -->
</script>
aspx.vb file:
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
回答by user3834074
you have to put this statement
你必须把这个声明
Me.WebBrowser1.ObjectForScripting = Me
inside form_load
里面 form_load
Private Sub form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.WebBrowser1.ObjectForScripting = Me
End Sub
see this site:- http://www.codeproject.com/Articles/35373/VB-NET-C-and-JavaScript-communication
请参阅此站点:- http://www.codeproject.com/Articles/35373/VB-NET-C-and-JavaScript-communication