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?
1) Form Design
2) Codebehind
3) When you click the delete button confirmation box will be displayed.
4) If you click ok then OK part will work in code else No Part
5) The same method in GRIDVIEW DELETE BUTTON.
You can just put:
in the
ASPX
page, it does the same thing.Please try as follows. You have to return the result of the confirmation function (true or false).
If you want to excute some code in server side without actually making the post back, you have to use ajax to do that.
and have a page called ajaxserverpage.aspx and in the page load of that page, check the query string and execute your relevant server side code and return some string
You can also use
generic handler (.ashx)
to do your ajax server side processing instead of the .aspx file. I would prefer this method.If you want javascript to access methods in the code behind you can always expose them in a RESTful service.
Have the REST call do whatever logic you need and send some data back to indicate a success. Then just use the successfunction to remove the element from the DOM.