Sure thing, it's always helpful to provide some sample code.
Just a reminder, the only option you have available is to use the confirm function in JavaScript.
Unfortunately, using a jQuery UI dialog or any similar tool won't work because they don't pause the code execution.
For example, you can include a basic button like this:
<asp:Button ID="Button1" runat="server" Text="Button" Width="118px"
OnClientClick="return confirm('delete this');"/>
By doing this, a confirmation dialog will appear:
https://i.sstatic.net/j9O3q.png
If the user clicks OK, the server-side client code will execute. If they click Cancel, the button action will be aborted. This method works well for confirmation dialogs, like when you need to confirm a deletion action.
Keep in mind, using jQuery UI or similar plugins won't have the same effect, as these dialogs won't pause the code execution. You can still use JavaScript dialogs or prompts, but the process will need to be adjusted slightly.
Using confirm('some message') for a confirmation prompt is effective, and if the user cancels, the server-side code won't be triggered.
You can also pass parameters in the client-side event, but it depends on the context (e.g., for a data-bound control). So, the implementation may vary depending on the scenario.
For instance, if you're dealing with a gridview or a similar component...
Remember, the recommended approach is to select one method, learn it thoroughly, and stick with it.
I would suggest using jQuery UI, especially if your site already has jQuery installed. Adding jQuery UI for appealing dialogs won't require much additional effort.
Now, let's see how this can be achieved with a jQuery UI modal dialog.
Here is the necessary markup:
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<asp:Button ID="cmdDelete" runat="server" Text="Delete record" Width="118px"
OnClientClick="return mydelprompt(this)"/>
<br />
<div id="mydeletediv" style="display:none">
<h2>Do you really want to delete this</h2>
</div>
<script>
mydelpromptok = false
function mydelprompt(btn) {
if (mydelpromptok) {
mydelprompt = false
return true
}
myDialog = $("#mydeletediv")
myDialog.dialog({
title: 'Confirm delete',
modal: true,
width: '400px',
appendTo: "form",
position: { my: "left top", at: "right bottom", of: btn },
buttons: {
Delete : function () {
myDialog.dialog('close')
mydelprompt = true
btn.click()
},
cancel: function () {
myDialog.dialog('close')
}
}
})
return false
}
</script>
Here is the result:
https://i.sstatic.net/CKPWt.png
Feel free to customize the content within the prompt div - you can add images, text boxes, checkboxes, etc. Whatever you include will be displayed in the dialog.
If the user confirms deletion, the server-side button click event will be triggered. If they cancel, the code behind the button click won't execute.