Reputation: 221
This is my sciprt, it display a message box with an 'Ok' and 'Cancel' Button
<script type="text/javascript"> function Confirm() {
var confirm_value = document.createElement('INPUT');
confirm_value.type = 'hidden';
confirm_value.name = 'confirm_value';
if (confirm('Continue?')) {
confirm_value.value = 'Yes';
} else {
confirm_value.value = 'No';}
document.forms[0].appendChild(confirm_value);} </script>
On my program i'm running a query, if there are no results then i display this "Dialog" box
I want to call the function immediately after getting the query results but my current code seems to be running it after everything instead of immediately.
If reader.read = false then
If Not Page.ClientScript.IsStartupScriptRegistered(Me.GetType(), "alertscript") Then
Page.ClientScript.RegisterStartupScript(Me.GetType(), "alertscript", "Confirm();", True)
End if
Dim confirmValue As String = Request.Form("confirm_value")
If confirmValue = "Yes" Then
'Do stuff here
End if
End if
Upvotes: 0
Views: 294
Reputation: 416121
To me, this request usually represents a misunderstanding of what's going on. At the time your VB.Net code is running, the javascript doesn't exist. All server event handlers result in a full postback. That means the entire page is recreated from scratch. The VB.Net code here is part of a process that is generating an entirely new HTML document. That will involve the entire page lifecycle, including your server's Page_Load code. When the event was raised, any html already rendered in the browser was destroyed to make way for your response to a whole new HTTP request.
If you want this to respond differently, you need to build your whole HTTP response with that in mind. That means either changing how the event is raised from the outset (calling a WebMethod or other ajax request) or setting your response to call your confirm method in the javascript page load event.
Upvotes: 2