JavaScript return values accessible to ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there an elegant way that I can return a value from a JavaScript function
to ASP.NET? I want to have a popup confirmation dialog using JavaScript and
only have a button control execute its ASP code if the confirmation is
positive. I can associate the JavaScript with the button using the Attributes
property and get the confirmation dialog to appear but the button control
always executes.
 
something like:

myButton.Add("onclick","return confirm('Are you sure you wish to do
this?');");

MattC
 
MattC said:
something like:

myButton.Add("onclick","return confirm('Are you sure you wish to do
this?');");

MattC

It's better to use "if (!confirm('Are you sure?')) return false;"
(don't forget the last ";" !)
especially for imagebuttons and linkbuttons, because asp.net will
add it's own functions to that same onclick, and you *do* want
those to execute when the question is confirmed.
 
I can see that the JavaScript is returning a value but how do I consume it
from within some C# code behind? Suppose I have a C# click event handler that
executes a stored procedure and the same button has this JavaScript confirm
assigned to it via mybutton.Attributes.Add. The C# code seems to execute
after the JavaScript regardless of the script return value. I can't see how
to test the JavaScript return value in order to prevent the C# code from
executing or call a different C# method upon a true return value.
 
Sorry, this does work as suggested. However, I'd really like to understand
why the C# code doesn't execute (which is the right behaviour) upon a
returned false value. What mechanism handles the returned script value?
 
Back
Top