Call javascript from vb.net code behind

Troy picture Troy · Oct 6, 2011 · Viewed 58.2k times · Source

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?

Answer

Deeptechtons picture Deeptechtons · Oct 6, 2011
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