Is it possible to prevent a postback by utilizing JavaScript's confirm function?

I am currently working on an aspx page that contains an asp.net button. The code for the button is shown below:

<asp:LinkButton ID="btn_Delete" runat="server" OnClick="btn_Delete_Click" OnClientClick="ConfirmDelete();" Text="Delete" /> 

The Confirm delete function is as follows:

   function ConfirmDelete() {
       var answer = confirm("Are you SURE you want to delete this item?");
       if (!answer) {
           return false;
       }
   }; 

I originally thought that clicking cancel would prevent the page from posting back, but it still seems to be posting back. Is there a way to prevent postback using the confirm function?

Answer №1

To get the desired outcome in the event handler, make sure to return the value from the function:

OnClientClick="return ConfirmDelete();"

Simplify the logic in the function by just returning the result of the confirm call:

function ConfirmDelete() {
  return confirm("Are you ABSOLUTELY certain you wish to remove this item?");
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interactive form fields and showcasing data

Hi there, I'm not very familiar with jQuery but I need some help with the following code: jQuery(function($) { $('#precoInserido').bind('keydown keyup keypress', function() { $('#precoVer').html(this.value || "??") ...

Looking to trigger the Enter key press with .sendKeys(Keys.ENTER); on something other than a text field

There is a unique element on the page which is neither a text box nor a button, it consists of a list of items. Strangely, pressing enter on the webpage causes my list of orders to disappear from the user interface. I attempted to utilize driver.findEleme ...

Understanding this JavaScript code involving shorthand if statements and commas can be achieved by breaking it down step by

Upon encountering this function within the codebase (which is compiled with webpack), my curiosity was piqued and I wanted to delve into its workings. Initially, my eyes fell upon t.length > 100. If it's greater than 100, then the next condition i ...

javascript array push not functioning as expected

I have written some JavaScript code that is supposed to add another element with a value of 1 when I click a button. However, currently it is resetting the array so only one element gets added each time. How can I fix this issue? var x = document.getElem ...

Monitoring the validity or errors in AngularJS input fields

I'm attempting to observe the $error or $valid status of a control. Here is the control in question: <form id="myForm" name="myForm"> <input name="myInput" ng-model="myInputMdl" business-validation/> </form> The business-validat ...

Utilizing Vue alongside Laravel by passing null values as props

Is it permissible to provide a null prop? I have created a main component that accepts the user prop. <div id="app"> <master :user="{{ $user }}"></master> </div> The prop is declared as follows: props : { user: { ...

Why is the 3rd argument necessary when using useReducer?

According to the information provided in the documentation: [init, the 3d argument] allows you to separate the logic for determining the initial state outside of the reducer. This is particularly useful for resetting the state later in response to an ac ...

WordPress API Encounters a 400 Error with OAuth

While experimenting with the WordPress Rest API, I decided to download a plugin called WP OAuth Server by Justin Greer and managed to set up my own OAuth connection. However, I encountered an issue where I received Error 400 indicating that The grant type ...

Avoiding state transitions within Angular's ui-router from within a directive

One of the challenges I am facing involves a directive called clickable-tag, where I pass my data as the tag's name (tag.tag): <a class="item item-avatar" ui-sref="nebula.questionData({questionId: question.id})" ng-repeat="question in questi ...

Save information for each session with a set expiration time

Currently, I am working on a mobile application using Angular (specifically Ionic 5) and I am in need of a solution to maintain session data for my users throughout their workflow. Initially, I thought about utilizing the sessionStorage feature for this p ...

I encountered an issue when sending a PATCH request via Hoppscotch where the request body content was returned as 'undefined', although the username and ID were successfully

const express = require("express"); const app = express(); const path = require("path"); let port = 8080; const { v4: uuidv4 } = require('uuid'); app.use(express.urlencoded({extended: true})); app.set("views engine", ...

Encountering Webpack issues following the transition to Next 13

Since updating Next to version 13, we've encountered issues with our application not building properly. It appears that webpack is having trouble with imports, exports, and potentially typescript. ../../libs/queries/src/lib/groq/searchFaq.ts Module pa ...

"Enhance your visuals with a rotating light source in combination with a camera and Orbit

In my Three.js scene, I've integrated OrbitControls.js for rotation and panning functionality. However, I'm facing an issue with the lighting setup - I want the lighting to move along with the camera, ensuring that the object is always well-illum ...

.When opening an ASPX page, the RDLC report fails to display

I am facing an issue with my .RDLC report not displaying on the ASPX page. Here is my ASPX code: <rsweb:ReportViewer ID="rvReport" runat="server" Height="500px" Width="100%" Font-Names="Verdana" Font-Size="8pt" InteractiveDeviceInfos="(Collection) ...

Is it possible to compare two charts in Chart.js in a way that avoids the issue of small values appearing as large as big values?

I am currently working on a production tracking page that features multiple charts. I want to avoid inconsistencies in tracking at first glance. Is there a way to achieve this using chart.js? If not, what would be the best approach to address this issue? ...

Quickest method to retrieve a user's assigned roles

I have a website built on ASP.Net MVC 5 and I need to retrieve the roles of the current user in order to take certain actions based on those roles. Despite some updates since the Beta version of VS 2013, I have been using the following code: //inside U ...

Validation of calendar input in ASP.NET

Utilizing a textbox to capture the selected date from a calendar, followed by implementing a range validator as shown below: Calendar1.SelectionMode = CalendarSelectionMode.Day Calendar1.SelectedDate = Date.Today rvDate.ControlToValidate = "txtValidate" r ...

Transforming a JavaScript variable into a PHP function call

Looking to transfer a Javascript variable into PHP by replacing the const 4 with var i var ntime = '<?php echo count($czasAr);?>'; for (var i = 0; i < ntime; i++) { ...

Validating date parameter in Wiremock request - How to ensure dynamic date matching during testing?

Looking for some assistance in verifying that the date in a request is exactly Today. I've tried various methods from the documentation, but haven't achieved the desired outcome yet. Calling out to any helpful souls who can guide a junior QA thro ...

How to achieve a one-time use of JQuery scrollTo for scrolling to a specific

Currently, I am in the process of developing a website where I showcase my child pages using a slideshow on the main parent page. Each child page has a scroll down link that should smoothly transition to the first title above the image when clicked. Howeve ...