I have a form where a user can delete a record, and I want a pop up message where the user has to click okay to confirm the delete.
Delete button:
<asp:Button ID="btnDelete" runat="server" Text="Delete" UseSubmitBehavior="false" OnClick="btnDelete_Click" OnClientClick="confirmation();" />
Confirmation function:
function confirmation() {
var answer = confirm("Are you sure you want to delete? This action cannot be undone.")
}
So right now, clicking the delete button executes the btnDelete_Click Sub in the code behind regardless of whether you click okay or cancel in the pop up box. I know I can add if (answer) { -- some code here -- } in my javascript function, but is it possible to use javascript to execute code from the codebehind? Or is there another way to do this?
We can easily do this by using ConfirmButtonExtender,
Its very simple...
Put this in your aspx code:
I know this is old post, but you can put the above answers into one line like this. And you don't even need to write the function.
Yet another way to achieve this would be using the AJAX Toolkit ConfirmButton Extender, as shown here:
http://www.ezineasp.net/Samples/ASP-Net-AJAX-cs/Control-Toolkit/AJAX-ConfirmButton-Control/Default.aspx
please use this sample:
I think above should work if you are not able to use it with Button, Try the
<asp:link button>
. Mine works just fine.ASPX page:-
VB server codebehind:-
One thing I noted is that if you are using control (ascx) within a page you may need to to off/on at page or control level
AutoEventWireup="false" <%@ control in <%@ page
Good Luck !!