calling JavaScript function with parameters from VB from Web Forms page

maqskywalker
maqskywalker used Ask the Experts™
on
Hi experts,

I'm trying to learn to call a JavaScript function from Code in ASP.NET Web Forms and VB.

I created 2 examples.

Example 1 works fine.
In example 1 I'm just calling a javascript function from the code behind.

Example 2 i'm having an issue with.
In example 2 i'm passing some parameters from the code behind to the javascript function.
How do I fix example 2? How do I correctly pass parameters from code behind to a JavaScript function?


Example 1: Call JavaScript function from Code behind

Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="JavaScriptsEventsVB._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript">
       function myFunction() {
           // show message in alert window
           alert('Function called successfully!');
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h4>
        Call JavaScript function from code-behin in Asp.net</h4>
    <div>
        <asp:Button ID="btnServerSide" runat="server" OnClick="btnServerSide_Click" Text="Call Function" />
    </div>
    </form>
</body>
</html>

Open in new window


Default.aspx.vb


Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)

        ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", "myFunction();", True)

    End Sub


End Class

Open in new window



Example 2: Call JavaScript function from Code behind with parameters

Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="JavaScriptsEventsVB._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type="text/javascript">
       function addFunction(intparam1, intparam2) {

           var firstNumber = intparam1; 
           var secondNumber = intparam2; 
           var answer; 
           answer = firstNumber + secondNumber;
           // show answer in alert window
           alert(answer);
       }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h4>
        Call JavaScript function from code-behin in Asp.net</h4>
    <div>
        <asp:Button ID="btnServerSide" runat="server" OnClick="btnServerSide_Click" Text="Call Function" />
    </div>
    </form>
</body>
</html>

Open in new window



Default.aspx.vb

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnServerSide_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim testparam1 As Integer
        Dim testparam2 As Integer
        testparam1 = 4
        testparam2 = 7

        ' call a JS function from vb code and pass parameters to i
        ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", "addFunction(testparam1, testparam2);", True)


    End Sub


End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Freelance IT Consultant
Commented:
Try to change this line
ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", "addFunction(testparam1, testparam2);", True)

Open in new window


to
ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", String.Format("addFunction({0}, {1});", testparam1, testparam2), True)

Open in new window


You need to pass parameter values, not names.

Author

Commented:
thanks. worked great.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial