Javascript Confirm on ASP.Net Button
August 11, 2008
Do you trust users? I don’t. Its easier than you think to attach a little javascript confirm popup to a proper ASP.Net server side button. Thanks to the OnClientClick event handler we can write our javascript right in the line and when the user thinks better of what they’re doing and clicks cancel the POST action will be canceled by the popup returning false.
It’s not a fancy AJAX modal thing but its a quick and easy solution to a common problem. Hope it helps someone.
10 Comments
leave one →
Just what I was looking for. Thanks! The AJAX control is nice but much heavier than I needed.
Wouldn’t you be better off to say
OnClientClick=”if(!confirm(‘Are you quite sure?’)) return false; ” ?
That way you wont return without running validation etc. if the user clicks ok.
Yeah, Mike’s variation works for me.
The original code shows the dialog but ignores the user’s response 🙂
Au contraire… Original code works for me, while Mike’s don’t.
Mike’s code work fine! Thanks Mike :] You’re cool guy ;]
Quite Useful Tip; I am developing a project in .net, after completing the project partially I found that MSGBOX control does not work on the web Site, So it has served my purpose. by embedding the script in the html code (client side) I could solve the
problem
Thanks
Exactly right Mike’s is correct. The other code will block the request but won’t send the request if OK is clicked.
When i click delete button without checking an email it should first say “Please select”..But first my javascript function onclientclick is calling as “Are you sure to delete” and if i click ok then it says lbltext = “Please select”. What should I do to validate first the delete button onclick event and then the javascript onclientclick event?
This works fine from a button. But I need a confirm message from code. Could someone point the way. Thanks
Very well written article. It will be helpful to anyone who usess it, as well as yours truly dabgecccbedd