passing string parameter to JavaScript function from VB code behind

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.


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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
   <script type="text/javascript">
       function employeeFunction(strparam1) {

           // show string parameter in alert window

    <form id="form1" runat="server">
        Call JavaScript function from code-behin in</h4>
        <asp:Button ID="btnServerSide" runat="server" OnClick="btnServerSide_Click" Text="Call Function" />

Open in new window


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

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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
what if trying:

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

Open in new window


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
Julian HansenCommented:
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?
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)
maqskywalkerAuthor Commented:
Thanks for the info. good info.

Good info Julian.
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
Visual Basic.NET

From novice to tech pro — start learning today.