Calling vb.net function from javascript
39,031
Solution 1
Here is a link that shows how to do it: Call VB method from JavaScript
It basically says there are 2 ways, Ajax or Postback. Here is the postback method:
aspx file:
<script type="text/javascript">
<!--
function callServersideFunction()
{
var someValueToPass = 'Hello server';
__doPostBack('CustomPostBack', someValueToPass);
}
// -->
</script>
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
Solution 2
you have to put this statement
Me.WebBrowser1.ObjectForScripting = Me
inside 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
Author by
Krunal Shah
Updated on March 10, 2020Comments
-
Krunal Shah about 4 years
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>
and here is my vb.net code
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'sorry for my uneven formatting of the code...i am new to stackoverflow...