Javascript working with ASP.NET code behind
11,478
Solution 1
You would simply prevent the callback from happening if the user hit cancel
Edit: as Josh Stodola pointed out
<asp:Button runat="server" ID="Save"
OnClientClick="if (!confirm('Are you sure?')) return false;" />
Solution 2
You want to do something like:
<asp:button runat="server" id="mybutton"
OnClientClick="return confirm('delete?');" />
Related videos on Youtube
Author by
Admin
Updated on April 17, 2022Comments
-
Admin about 2 years
I have to make a delete button for a simple intranet forum. I would like to have a javascript alert box (or equivalent) asking for confirmation when deleting posts. However, I've found it difficult to get the result of the javascript confirmation box to feed into the code-behind. Is this even possible? Or do I have to use another sort of design?
-
Josh Stodola about 15 yearsYou should only return from the click event if the user clicks cancel (if the confirm function returns false). vaultofthoughts.net/OnClientClickBreaksValidation.aspx
-
NotMe about 15 years@Josh: I didn't know that. Probably because I've never bothered with .Net's client side validation. Thanks for the link / info.