7

I have written VB.NET code for calling my Javascript function showDisplay().

vb.net code:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, GetType(Page), "Script", "showDisplay();", True)

javascript code:

function showDisplay(){
alert('success');}

but this is not working, can you help?

Nalaka526
  • 11,278
  • 21
  • 82
  • 116
Novus
  • 101
  • 1
  • 2
  • 9
  • possible duplicate of [How to call javascript function from code-behind](http://stackoverflow.com/questions/4848678/how-to-call-javascript-function-from-code-behind) – The Hungry Dictator Jan 20 '14 at 05:52

1 Answers1

15

Perhaps you are looking for RegisterStartupScript:

ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "showDisplay();", True)

Depending on where your showDisplay() javascript function exists in your code, using RegisterClientScriptBlock may not find it. This is because RegisterClientScriptBlock places the javascript at the top of your page, immediately after the viewstate. 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.

Aaron Palmer
  • 8,912
  • 9
  • 48
  • 77
  • if showdisplay message have arguments how to send it? – clarifier Feb 24 '16 at 13:10
  • 1
    Depends. Does the server side have knowledge of the argument values? If so, put them inline `showDisplay(\"StringValue1\", \"StringValue2\")`. Are the values only available on the browser? If so, provide variable names and ensure that the JavaScript contains these variables prior to calling `showDisplay(var1, var2)`. – Aaron Palmer Feb 24 '16 at 14:58
  • What if I have my JavaScript function wrote down under a different file? – krunal.cp Jun 23 '21 at 15:17