views:

1943

answers:

4

I have an ASP.NET page which has a button it it. The button click launches a modal dialog box using javacript. Based on the value returned by the modal dialog box, i want to proceed with, or cancel the post back that happens. How do i do this?

+11  A: 

Adding "return false;" to the onclick attribute of the button will prevent the automatic postback.

Wayne
If any exceptions are thrown before the return false, the automatic postback will still occur.
Chris MacDonald
A: 

Basically what Wayne said, but you just need to put 'return false;' in the function that presents the modal. If it's the value you want, let the postback happen. If not, have the function return false and it will stop the submit.

Tom
A: 
function HandleClick()
{
 // do some work;

if (some condition) return true; //proceed
else return false; //cancel;
}

set the OnClientClick attribute to "return HandleClick()"

chris
+1  A: 

Is this what you are trying to do?

<input type="button" id="myButton" value="Click!" />

<script type="text/javascript">
document.getElementById('myButton').onclick = function() {
    var agree = confirm('Are you sure?');
    if (!agree) return false;
};
</script>
Joe Lencioni