从javascript调用vb.net函数 [英] Calling vb.net function from javascript
本文介绍了从javascript调用vb.net函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要做的是从javascript调用vb.net函数
这里是我的html代码。
what I want to do is call a vb.net function from javascript
here is my html code.
<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
仍然按下按钮点击图片错误
未捕获TypeError:对象#没有方法'showVbHelloWorld'
still on button click im geting error
Uncaught TypeError: Object # has no method 'showVbHelloWorld'
抱歉我的代码格式不均匀...我是stackoverflow的新手...
sorry for my uneven formatting of the code...i am new to stackoverflow...
推荐答案
这是一个链接,显示如何这样做:从JavaScript调用VB方法
Here is a link that shows how to do it: Call VB method from JavaScript
它基本上说有两种方式,Ajax或Postback。这是回发方法:
It basically says there are 2 ways, Ajax or Postback. Here is the postback method:
aspx文件:
<script type="text/javascript">
<!--
function callServersideFunction()
{
var someValueToPass = 'Hello server';
__doPostBack('CustomPostBack', someValueToPass);
}
// -->
</script>
aspx.vb文件:
aspx.vb file:
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调用vb.net函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文