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?
Using RegisterStartupScript will place the call to showDisplay() at the very bottom of your form, so it will be rendered last and your javascript function will have already been rendered and available.
In reality, you don't need to embed any javascript into your vb.net code. Simply include a javascript file into your page and use the jQuery's . on() event handler.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With