calling JavaScript function with parameters from VB from Web Forms page

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

LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
thanks. worked great.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.