4

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...

2
  • Why are you using Javascript. Why not just use .NET event handling???? Commented Jun 15, 2013 at 11:01
  • im basically working on javascript...but i want to convert some of my html content to pdf using HiQpdf hiqpdf.com/demo/ConvertHtmlToPdf.aspx and HiQpdf provides c# and vb.net codes thats why i am trying this way... Commented Jun 15, 2013 at 11:05

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.