添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

i have a asp:button that will fire a delete and want to have a client side javascript are you sure pop-up prevent any accidents.

whats the javascript to handle this?

You can add the javascript to the OnClientClick() event of the button... the key is to return false if you want to cancel the event. If you return false, the OnClick will not fire.

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
            OnClick ="delete_clickhandler" 
            OnClientClick="return confirm('Are you sure you want to?');" /> 

Alternately, you can call a method in javascript

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
            OnClick ="delete_clickhandler" 
            OnClientClick="return MyDeleteConfirm();" /> 

Where MyDeleteConfirm() does something more elaborate, but returns false if you don't want to delete.

This worked for me (the accepted answer did not)

<asp:Button id="DeleteButton" runat="server" Text="Delete" 
OnClientClick="if (!confirm('Are you sure you want to delete?')) return false;">
</asp:Button>
                Strange that the code I posted didn't work for you... what you have should be more complicated than necessary.  Also, the point of my code is to suppress the OnClick event, and I don't see one on your button... how are you connecting the actual event to this button?  Wondering if that has something to do with it.
– James King
                Feb 10, 2016 at 8:14
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.