JavaScript return values accessible to ASP.NET

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.
 
M

MattC

something like:

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

MattC
 
H

Hans Kesting

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.
 
G

Guest

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.
 
G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top