calling JavaScript function from VB

I'm trying to call a JavaScript function from VB on button click.

Below is my code.
Why is the alert message not coming up when I click the button?

Call_JSFunctionFromVB1.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Call_JSFunctionFromVB1.aspx.vb" Inherits="TelerikWindow3Level.Call_JSFunctionFromVB1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function HelloFunctionVB1() {
            alert('Hello you called a JS function from VB')
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Button" />
    </div>        
    </form>
</body>
</html>

Open in new window


Call_JSFunctionFromVB1.aspx.vb


Public Class Call_JSFunctionFromVB1
    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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' ----- call JavaScript function -----
        Dim script As String = "<script language='javascript' type='text/javascript'>Sys.Application.add_load(HelloFunctionVB1);</script>"
        ClientScript.RegisterStartupScript(Me.[GetType](), "showWindow", script)
        ' ----- call JavaScript function -----

    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.

Paul JacksonSoftware EngineerCommented:
Why not just use the OnClientClick attribute of the Button control :

<asp:Button ID="Button1" OnClick="Button1_Click" OnClientClick="return HelloFunctionVB1()" runat="server" Text="Button" />

Open in new window


also need to add a return in javascript function as below, this just stops the form doing a server side submit. If you wanted it to execute the Server Side event Button1_Click then either take out the return false; or set it to return true; depending on your requirements.

<script type="text/javascript">
        function HelloFunctionVB1() {
            alert('Hello you called a JS function from VB');
            return false;
        }
</script>

Open in new window

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