2

I want to add a simple confirm, are you sure dialog when you click an asp:button which does postback. Something like:

OnClientClick="if(confirm('Format the hard disk?'))
alert('something');
else alert('something else')"

Problem is that whether you click ok or cancel it still posts back. How to make it so that it only posts back on OK?

5 Answers 5

1

If you're just looking to do a postback based on the selection from the confirmation dialog, this might be simpler:

OnClientClick="return confirm('Are you sure?');"
Sign up to request clarification or add additional context in comments.

1 Comment

Yes that's exactly what is needed. Simple and to the point :)
1

Also you can use next:

OnClientClick="return confirm('Delete this entity?');"

Comments

1

In short "....; return false;" stops the click, example below not using inline JS:

Markup:

OnClientClick="confirmSomething()";

JS Function:

function confirmSomething()
{
    var result = confirm('Format the hard disk?');
    if(result == 1)
    {
       alert('something');
       return true; //continue click event
    }
    else if(result == 0)
    {
       alert('something else');
       return false; //stop click event
    }
}

Comments

0

you need to return the confirm instead.

see: http://rumandcode.wordpress.com/2008/08/11/javascript-confirm-on-aspnet-button/

Comments

0

Use return false.

OnClientClick="if(confirm('Format the hard disk?')) alert('something'); else {alert('something else'); return false;}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.