passing string parameter to JavaScript function from VB code behind

maqskywalker
maqskywalker used Ask the Experts™
on
I'm trying to learn the syntax to call a JavaScript function from code behind and passing a string parameter to the JavaScript function.

I tried similar syntax passing an integer function and that worked fine

But something is not working when I try to pass a string parameter.

Below is my code.

What am I doing wrong?

When I click the button it should show the string "John Smith"  in the alert window.


CallJavaScriptFunctionFromVB.aspx

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

<!DOCTYPE html>

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

           // show string parameter in alert window
           alert(strparam1);
       }

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


CallJavaScriptFunctionFromVB.aspx.vb

Public Class CallJavaScriptFunctionFromVB
    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) Handles btnServerSide.Click

        Dim testparam1 As String
        testparam1 = "John Smith"

        ' ----- call a JS function from vb code and pass a string parameter to it -----
        ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", String.Format("employeeFunction({0});", testparam1), 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®
Software Team Lead
Commented:
what if trying:

ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", String.Format("'employeeFunction({0}');", testparam1), True)

Open in new window


?
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Just to clarify - you can't call a JavaScript function from Code Behind. When the server code runs - it runs to the end - renders out some HTML which is sent back to the browser. By the time the server code completes - the browser has not started up yet.

What you are doing is registering a startup script to do something when the page loads.

What you should consider using instead is when the button in your form is clicked - make an AJAX call back to the server and act on the return from that call instead of posting your form back.

The question you need to answer is - what are you trying to achieve with the way you are doing it currently. If you are going to render a page that as part of the render fires a JavaScript function to do something to the page - why not simply make that change in the page when it is rendered - why do you need to go to a JavaScript function at all with a post back?
leakim971Multitechnician
Top Expert 2014
Commented:
I think @Ryan is close, he just misplaced the first single quote :

ScriptManager.RegisterStartupScript(Me, [GetType](), "TestFunction", String.Format("employeeFunction('{0}');", testparam1), True)

Open in new window


(it should be before the open curly brace and not before the function name)

Author

Commented:
Thanks for the info. good info.

Good info Julian.

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