How can we determine from ASP.NET C# backend whether the user clicked OK or Cancel on the JavaScript confirm box?

Within the C# backend code for a website, I have implemented code that presents users with a confirmation message detailing the data they are about to submit. This message requires human-confirmation that the information displayed is accurate.

The challenge lies in accessing the outcome of the confirm function as my code is executed on the server-side in C#, not on the client-side in JavaScript. Depending on whether the user clicks OK or Cancel, my C# logic needs to branch into different paths.

I am currently working on an outdated application that has been around for 13 years without much modification. Therefore, I am unable to make any significant changes to the overall design of the application.


ClientScript.RegisterStartupScript(this.GetType(), "InvalidEntrySubTypeAlert", "confirm('" +
    "Are you sure you want to create the following deal?\n" +
    "\nDeal ID :\t\t\t" + nDealID +
    "\nCompany Name :\t\t" + txtCompanyName.Text +
    "\nEntry Subject :\t\t" + txtEntrySubject.Text +
    "\nBusiness Lead :\t\t" + ddlBusinessLead.SelectedItem.Text +
    "\nLicense Status :\t\t" + ddlLicenseStatus.SelectedItem.Text +
    "\nEffective Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEffectiveDate.SelectedDate) +
    "\nExpiration Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calExpirationDate.SelectedDate) +
   "\nLicense Location :\t\t" + txtLicenseLocation.Text +
    "\nEntry Sub Type :\t\t" + ddlEntrySubType.SelectedItem.Text.Split(' ')[0]
    + "');", true);

Answer №1

If you want to manipulate a value on the client side and then access it on the server, you can use a HiddenField.

Here's an example:

<asp:HiddenField ID="hndValueFromJS" runat="server"></asp:HiddenField>

You can set the value like this:

document.getElementById("#<%= hndValueFromJS.ClientID %>").value = "yourValue";

or

$("#<%= hndValueFromJS.ClientID %>").val("yourValue"); // Using JQuery

Answer №2

In my opinion, the best approach would be to invoke a page method (a backend method marked with the webmethod attribute) from the client side or utilize ajax to make a request to a web service.

Because the confirmation action is being carried out on the client side, there is no direct connection to your server-side code.

Answer №3

To implement two client-side functions, follow these steps:

function confirmAction()
{
//some code
}
function cancelAction()
{
    //some code
}

Update your existing code as shown below:

ClientScript.RegisterStartupScript(this.GetType(), "ConfirmationPopup", "if(confirm('" +
    "Are you sure you want to proceed with the following action?\n" +
    "\nID :\t\t\t" + nID +
    "\nName :\t\t" + txtName.Text +
    "\nSubject :\t\t" + txtSubject.Text +
    "\nLead :\t\t" + ddlLead.SelectedItem.Text +
    "\nStatus :\t\t" + ddlStatus.SelectedItem.Text +
    "\nStart Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calStartDate.SelectedDate) +
    "\nEnd Date :\t\t" + string.Format("{0:MM/dd/yyyy}", calEndDate.SelectedDate) +
   "\nLocation :\t\t" + txtLocation.Text +
    "\nType :\t\t" + ddlType.SelectedItem.Text.Split(' ')[0]
    + "')){ confirmAction();} else{cancelAction()}", true);

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

Explore the full range of events available on the Angular UI-Calendar, the innovative directive designed for Arshaw FullCalendar

Utilizing external events with Angular ui-calendar: HTML: <div id='external-events'> <ul> <li class='fc-event'>Event 1</li> <li class='fc-event'>Event 2< ...

JavaScript - Global Variable

Is it possible to declare and assign a value to a global variable in one JavaScript file and then reference it in another JavaScript file multiple times? Can Angular.js be utilized in a Velocity macro file, aside from HTML files? ...

Pass information to a JavaScript file without resorting to global variables

I am looking for a way to transfer information from an HTML page to a script file that is contained within the same page. My initial thought was to use a global variable defined in the page and then accessed in the script file. However, I understand that ...

Customizing the appearance of Previous and Next elements using JavaScript

My challenge involves working with a form on a webpage where I do not have direct access to the HTML or CSS. Therefore, any modifications need to be made using JavaScript\JQuery. The form in question has a layout like this: https://i.sstatic.net/Czuo ...

Error encountered upon initializing Node-RED due to the presence of an unexpected token while incorporating the NPM module "file-exists" into the

Currently, I'm in the process of developing an application using Node-RED and I'm looking to incorporate some NPM modules into my project. One particular module from James Thom caught my attention, called node-red-contrib-npm, which automates the ...

Learn how to showcase a text file uploaded to a webpage with NODE js and HTML

<!DOCTYPE html> <html> <body> <form action = "/submit" method = "post"> Select a file: <input type="file" name="file"> <input type="submit"> </form> </bod ...

Updating the country values in dropdown box1 using the onchange event with AJAX

I've hit a wall where I'm trying to populate the country after receiving an ajax response, but it's not displaying as selected. Here is the image for reference. If I select a country other than those in Africa, I should see all countries in ...

Is there a way to modify my code to restrict users from liking a post multiple times?

I am currently working on a like system and I have made some progress. However, I am struggling to make it so that the likes only increment once. Does anyone have any insights or suggestions on how to achieve this? I have considered using session variables ...

Tips for accessing the FormControlName of the field that has been modified in Angular reactive forms

My reactive form consists of more than 10 form controls and I currently have a subscription set up on the valueChanges observable to detect any changes. While this solution works well, the output always includes the entire form value object, which includ ...

Placing objects in an array results in the inclusion of numerous null values

Having trouble assigning an object to an array by key, as it is resulting in multiple null values alongside the actual object. To start off, I am outputting data to an HTML data-* attribute using PHP: <div id="{{$obj_id}}" data-my-obj="{ ...

Utilizing variable index to access nested objects

When my Ajax request returns a response, the data takes the form of an object shown below: https://i.sstatic.net/wA2Px.png How can I access the value? Keep in mind that idVariable is a variable. data.test1.idVariable.test2.value The result of the code ...

"Implementing Scrollify.js to dynamically add a class to the current section

I'm attempting to dynamically add an 'active' class to the current section on a page using scrollify.js. While I can retrieve the index value, I am struggling to get the section id or class. How can I obtain the id or class of the current s ...

The initial element within the div style is malfunctioning

Could someone assist me in understanding why the first-of-type CSS is not working correctly? .item:first-of-type .delete{ display: none ; } .delete { text-decoration: none; color: red; padding-top: 40px;} .add_form_field { white-space: nowrap; } < ...

Place the object into a vacant area with the help of jQuery Sortable

I am using jQuery Sortable and it's functioning well. However, I have encountered a small issue that I need help with.    I have two blocks where elements from one block can be moved to the other. The problem arises when all the elem ...

Issue encountered in loading css and js folders during the build of the Angular2 application due to the files not being found

I have developed an Angular 2 application that utilizes Node.js server APIs. After building the app using nd b, the files were generated in the dist folder. Where should I specify the production URL for the build so that all CSS and JS files load properly? ...

Activate or deactivate a button depending on the input value of a TextField in React.js

I am currently designing a form similar to the one displayed in this image. I want the "Add" button to become active as soon as the user starts typing in the "Tags" input field. For this project, I am using material-ui and I have created an Input compone ...

What is causing my divs to behave as if I set a margin-top/margin-bottom of one and a half centimeters?

I'm currently developing an AngularJS application and I've encountered some issues with unnecessary whitespace. <div class='user' ng-repeat='session in sessions'> <div class='text' ng-bind='monolog ...

Combining two-digit values

As a newcomer to JavaScript, I've been working on creating a basic calculator to practice my skills. However, I've run into an issue. When entering a double-digit number, the calculator is adding the digits together instead of treating them as se ...

Declaring an object in the form of a function

I need some assistance with calling a method that is declared like an object in the following code. What approach should I take to call the function properly? var draw = function() { anotherFunction(); } draw(); Upon execution, an error stating "Typ ...

I keep getting redirected to a blank page by JS

I've created a JavaScript script that smoothly fades in the page when a user enters it and fades out when they click a link to another page. The script is working as intended, but I'm facing an issue with anchor links on the page. Whenever I clic ...