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?');" />
Share:
11,478

Related videos on Youtube

Admin
Author by

Admin

Updated on April 17, 2022

Comments

  • Admin
    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
    Josh Stodola about 15 years
    You should only return from the click event if the user clicks cancel (if the confirm function returns false). vaultofthoughts.net/OnClientClickBreaksValidation.aspx
  • NotMe
    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.