1

I want to run a javascript alert popup inside a VB.net function. I have used something like this before, but I have only gotten it to work inside Page_Load or Page_Prerender. Is there something other than RegisterStartupScript that is used for my scenario?

edit: I added more detail as to what my situation is.

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click

    blnEditComplete = RunEditChecks()

End Sub


Function RunEditChecks() as Boolean

    'edit checks 
    ...
    ...
    ...

    ScriptManager.RegisterStartupScript(Me.Page, Me.GetType, "warning", "alert('WARNING: Price must equal Contract Price.');", True)

    ...
    ...
    Return True

End Function
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
madmike
  • 114
  • 1
  • 12
  • In which other event do you want to use this `RegisterStartupScript` that it isn't working? Click of a button? – Darin Dimitrov Jan 09 '17 at 20:24
  • No event. Just inside a function where I am doing some edit checks. – madmike Jan 09 '17 at 20:25
  • But who is calling this function? At the end of the day some event in your WebForm such as a button click or something provoked the call to this function, right? – Darin Dimitrov Jan 09 '17 at 20:25
  • The function is called inside a button click event. But I would like to display the message at a specific point in my edits. That is why I don't want to add the alert to the button click event itself. – madmike Jan 09 '17 at 20:27
  • But in order to call the `RegisterStartupScript` function you need a reference to the `ScriptManager` instance, which in turn is very ASP.NET specific and you probably don't want to pollute your business methods with it. In this case you could have your business methods just take an event/delegate that will be invoked when the appropriate moment comes and the event passed from the button click handler will contain the appropriate call. – Darin Dimitrov Jan 09 '17 at 20:31
  • You cannot execute client-side javascript `alert` "in the middle" of server-side method. – Igor Jan 09 '17 at 20:39
  • i thought I had seen it done before. Maybe I'm thinking of something else. – madmike Jan 09 '17 at 20:41
  • @madmike you can assign string variable and give it the alert between the script tag but this will show the alert on page load – Amr Ashraf Jan 09 '17 at 20:46
  • Yeah, that's not what I'm trying to do. Thanks anyways everyone. – madmike Jan 09 '17 at 20:51
  • @madmike your question was asked before http://stackoverflow.com/questions/21227026/how-to-call-javascript-function-from-vb-net-code – Amr Ashraf Jan 09 '17 at 21:04
  • Possible duplicate of [Calling javascript from code behind](http://stackoverflow.com/questions/1828566/calling-javascript-from-code-behind) – Heretic Monkey Jan 09 '17 at 21:37
  • @madmike in your code replace Me.Page with Me and Me.GetType with Page.GetType – Amr Ashraf Jan 09 '17 at 21:38
  • I realized it may come across as a duplicate, but with my specific situation I thought i'd ask anyways. – madmike Jan 10 '17 at 15:08

1 Answers1

0

JS function:

function showDisplay(){
    alert('WARNING: Price must equal Contract Price.');
}

vb.net:

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

Duplicate question: link

Community
  • 1
  • 1
Amr Ashraf
  • 315
  • 4
  • 14