Ensure that confirmation is only prompted in ASP.NET when necessary

I am faced with a scenario where I need to handle the value of Session["actionMode"] in conjunction with a button click event. Here is what I currently have implemented in my code:

protected void Button1_Click(object sender, EventArgs e)
{

    if ((int)Session["actionMode"]==1)
    {
      //Perform a specific action
    }
    else if ((int)Session["actionMode"]==3)
    {
       //Proceed with deleting a record after confirmation
    }

}

Upon analyzing the logic, it is evident that when Session["actionMode"] is set to 3, a record deletion operation is triggered. However, prior to executing this deletion, I want to ensure that the user confirms this action. My question then arises - is there a way to implement a confirmation prompt in cases where Session["actionMode"] is not equal to 3, thereby avoiding the need for unnecessary confirmations? Could JavaScript potentially resolve this issue?

Answer №1

This specific piece of code...

{ 
    //Once the confirmation is received, proceed to delete a record 
}

You are currently running server side code that requires user interaction.

No matter what action you take (client or server), the process must be split. Let's explore a server side approach.

{ 
    // Show a confirmation message with server side controls.
}

protected void YesDeleteTheDamnRecord_Click(object sender, EventArgs e)
{
    // Proceed with the deletion process.
}

Alternatively, you can consider implementing a solution with client side interaction.

  • Verify the value of "actionMode" prior to displaying Button1.
  • If it matches "3", assign a client side event handler to Button1.OnClientClick.
  • Show a confirmation message (using window.confirm, for example).
  • If the user confirms by clicking "Yes," execute the necessary server side code to delete the record.

Answer №2

For removing data, I recommend utilizing the following method:

protected void Button1_Click(object sender, EventArgs e)
 {
      if ((int)Session["actionMode"]==1)
     {
       //Execute code block for actionMode equals 1
     }
     else if ((int)Session["actionMode"]==3)
     {
        //Invoke JavaScript function to trigger Ajax request
     }
  } 

JavaScript:

function callAjax()
{
   if(confirm("Do you want to delete data?"))
   {
     //Make Ajax call here
   }
   else
   {
     return;
   }
}

Ajax Page:

PageLoad
{
    Retrieve value of Session["actionMode"]
    Perform the deletion process...
}

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

Checking to see if the prop 'isChecked' has been modified

I'm currently facing a challenge with testing whether a class's prop value changes after clicking the switcher. Below is the component class I am working with: import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core&a ...

Slow execution of Bootstrap v4 modal display

It has come to my attention that the modals in Bootstrap show slower as the page's content increases. When the page is empty, it only takes less than 100ms to show. However, the time it takes significantly increases as more content is added to the pa ...

What causes non-reactive variables to act unusual in Vue3?

Check out this code snippet: <template> <input type="button" value="click" @click="clickBtn"/> <div id="reactiveDiv">{{ reactiveVariable }}</div> <div id="noneReactiveDiv"&g ...

Enhancing nested mongoose arrays by a particular value for updates

In my mongoose schema, I have an array that contains items and another array. var mongoose = require('mongoose'); var Schema = mongoose.Schema; var CompanySchema = new Schema({ dateCreated: { type: Date, default: Date.now }, ownerId: { typ ...

Decoding the Blueprint of Easel in JavaScript

Recently, I came across a fantastic API that promises to simplify working with CANVAS by allowing easy selection and modification of individual elements within the canvas. This API is known as EaselJS, and you can find the documentation here. While I foun ...

The ngOnChanges method fails to exhibit the anticipated modifications in a variable

Trying to grasp the concept of the ngOnChanges() callback, I created an example below. Despite having values for the attributes title and content in the Post interface during compile time, I do not see any logs from ngOnChanges. Please advise on the corre ...

Create a single line of content for a carousel display

Seeking a solution where I can have a container that slides sideways without stacking in rows, I have exhaustively searched for answers using different keywords like single row, inline, flexbox, and grid but to no avail. Any assistance will be highly appre ...

Searching Text Boxes with Javascript: A Better Way to Handle Arrays

I'm struggling to implement a feature where users can search for authors in a database and be redirected to the corresponding HTML if found. Otherwise, it should display a message saying "No Author Found"... I need some assistance in getting this fun ...

Utilize Jquery to insert new text into a textbox and then organize the contents of the textbox using Jquery

Currently, I am utilizing the ASP.NET asp:checkboxlist control which consists of 36 listitems, displaying 36 checkboxes. The HTML representation resembles a table format. My goal is to dynamically add checked items in this div in a sorted manner. Additiona ...

Including an <a> tag within a JavaScript variable to utilize in an HTML statement

I want to wrap an <a> element around a JavaScript variable containing a string. The desired output should be the 'copyright symbol' followed by the 'companyName' linked as an HTML link and then the current year's date. JavaS ...

What is the process for adding parameters to a Fetch GET request?

I have developed a basic Flask jsonify function that returns a JSON Object, although I am not certain if it qualifies as an API. @app.route('/searchData/<int:id>',methods=["GET"]) def searchData(id): return jsonify(searchData(id)) Curr ...

Elasticsearch query fails to execute when encountering a special character not properly escaped

I am facing an issue with querying a keyword that includes a dot (.) at the end. While the query works perfectly on Kibana's console, it fails to execute on my application's function. The following function is responsible for creating the query b ...

Searching for the clicked tr element within a tbody using jQuery

Here is my customized HTML table layout <table> <thead> <tr> <td class="td20"><h3>Client Name</h3></td> <td class="td20"><h3>Details</h3></td> ...

Change the local date and time to UTC in the format of yy:mm:dd H:M

I must change the date format from local time to UTC or ISO as yy:mm:dd H:M, or calculate the difference between local date times with 03:30 as yy:mm:dd H:M 2016-10-22T04:30:00.000Z convert this to 2016-10-22T01:00:00.000Z ...

Navigation menu with submenus containing buttons

I attempted to incorporate a dropdown into my existing navigation bar, but unfortunately, the dropdown content disappeared after adding the necessary code. I am now at a loss on how to troubleshoot this issue and make the dropdown function properly. Despit ...

Failed installation of ASP.NET Core Blazor Language Services

I am excited to dive into the new Blazor platform using VS 2017 community edition, but I am facing an issue with installing the extension. Below is the log file that was generated during my attempt to install the plugin. My .NET framework version is 4.7.x ...

Using JQuery to Send Form Data with an Ajax POST Request

On my web Node/Express app, I have implemented a basic messaging service and am currently attempting to submit the form using Ajax with the FormData object. While the form submission works perfectly without Ajax, all the req.body values are undefined when ...

Highlighting JavaScript code with React Syntax Highlighter exclusively

I've been struggling to highlight Ruby code, but it seems like no matter what I do, it keeps highlighting JavaScript instead. It's frustrating because I can't seem to get it right. import React from "react"; import atom from "node_module ...

Error message: The function for the AJAX call is not defined

I'm currently working with jQuery's ajax function and encountering an error: 'getQty is not defined' Can you spot my mistake here? jQuery Code: function getQty() { var dataString = "itemId=" +$(".itemId").val(); $.ajax({ t ...

Getting HTML from Next.js middleware - a step-by-step guide

Is there a way to send the HTTP Status Code 410 (gone) together with a customized HTML message? I want to display the following content: <h1>Error 410</h1> <h2>Permanently deleted or Gone</h2> <p>This page is not foun ...