Call javascript from vb.net code behind
Asked Answered
F

2

9

How can I call a javascript function from code behind?
The most popular response is "ScriptManager.RegisterStartupScript" however, that does not work in my situation.

I have a vb class that is doing a database check to see if a record exists. If exists, then call a javascript function to display an alert("Record exists")

So I am doing something like

Dim strMessage as string = "javascript:RecordExists('Param');"  

How do I call this function from my vb.net class?

Follansbee answered 6/10, 2011 at 11:46 Comment(1)
possible duplicate of Passing arguments to JavaScript function from code-behindDurarte
R
13
If DataStore.Record.Exists(theRecord) Then

    Dim script As String = "alert('Record exists')"
    If Not Page.ClientScript.IsStartUpScriptRegistered(Me.GetType(), "alertscript") Then
        Page.ClientScript.RegisterStartUpScript(Me.GetType(), "alertscript", script, True)

    End If
End If

you would do it like above, where you should replaceDataStore.Record.Exists(theRecord) with condition that checks database record exists

Rodman answered 6/10, 2011 at 11:54 Comment(4)
when I attempt to use your solution, I get an error "Object reference not set to an instance of an object" at "If Not Page.ClientScript....."Follansbee
error message: 'Page' is not a member of 'System.Web.HttpContext'.Follansbee
Mistakes in the previous post:<br/> If DataStore.Record.Exists(theRecord) Then<br/> Dim script As String = "alert('Record exists');"<br/> If Not Page.ClientScript.IsStartUpScriptRegistered(Me.GetType(), "alertscript") Then<br/> Page.ClientScript.RegisterStartUpScript(Me.GetType(), "alertscript", script, True)<br/> End If<br/> End IfFreelance
Is there any way to call javascript function that is already written on front end, i mean at aspx file. I just want to make call to that function from code behind.Shabbygenteel
M
11

You need to think about your script in a slightly different way - remember, JavaScript runs client-side, and VB.NET runs server-side. So you can't "call" JavaScript from the server side.

However, you can generate JavaScript on the server side, but it will need to be output to the page before it can run.

If you were doing a full page postback, a crude way of achieving it would be to assign the script or function to a Literal control, which renders its Text property on the HTML page exactly as written.

Then, your script will execute at the point the Literal is rendered.

A neater way of doing it is to add your script to the page via a ScriptManager as you noted. Rather than a StartupScript, you could try using .RegisterClientScriptBlock() instead? You don't mention what it is about your situation that doesn't work?

The most comprehensive way of doing it would be to use AJAX - either .NET's built-in framework, or jQuery. jQuery's AJAX (and AJAX in general) is a separate topic, which you can read about here.

Myles answered 6/10, 2011 at 11:58 Comment(2)
prohibited from using JQuery... :-(Follansbee
That's OK - it's just a nice-to-have. Take another look at using the ScriptManager first, and if that really doesn't work for you, then browse the net for .NET AJAX samples.Myles

© 2022 - 2024 — McMap. All rights reserved.