Getting the return value from a confirm box in ASP.NET C#

Despite reading through numerous similar questions and answers, I am still unable to find a solution to my problem. I am working on a form that allows users to select a file and choose a date for importing the file. If the selected date is before the last import period stored in the database, I want to display a confirm box to alert the user about potential data overwriting. The user can then choose to proceed with the import or cancel it.

I have attempted using the onClientClick method of the asp:Button control, but the confirm box pops up immediately upon clicking the submit button. I need the confirmation box to appear only after checking the last import period with server-side C# code. I even tried storing the return value in a hidden field, but that didn't work.

I have explored solutions like the one mentioned in this thread but have encountered issues due to the use of an updatePanel. My setup only includes a DatePicker, a DropDownList for selecting the client, and a hidden field if needed. Any help or suggestions for tweaking the existing code or implementing a new solution would be greatly appreciated. Thank you.

Answer №1

If you're looking for a solution, give this code a try:

var result = confirm('Are you sure you want to proceed?');
if (result == true) {
    document.getElementById('<%= HiddenField1.ClientID %>').value = 1;
} else {
    document.getElementById('<%= HiddenField1.ClientID %>').value = 0;
}

This snippet allows you to manipulate the HiddenField element in your server-side code.

Answer №2

Give this a try for a hidden field value of yes or no

<script type="text/javascript>
    function ConfirmSave() {
        var confirmValue = document.createElement("INPUT");
        confirmValue.type = "hidden";
        confirmValue.name = "confirm_value";
        if (confirm("Do you want to save the data?")) {
            confirmValue.value = "Yes";
        } else {
            confirmValue.value = "No";
        }

        var confirmField = document.forms[0].appendChild(confirmValue).value;
        //alert(confirmField);
         var hiddenField=document.getElementById('<%= hiddenfeid.ClientID %>').value
         hiddenField = confirmField;
        // alert(hiddenField);

    }

</script>

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

Duplicate a user interface control

In my ASP.NET Web Forms project, I have created a custom .ascx user control that represents a Car with properties such as Type, Picture, and Color. Now, I am trying to display multiple instances of this custom control on the user interface. For example, i ...

Creating sitemaps for multi domain websites using NextJS

We are implementing a next-sitemap package to generate sitemaps for our Next.js pages located in the /pages directory. For pages that come from the CMS, we use server-sitemap.xml with SSR. Despite having 6 different domains, we manage them within a single ...

Ajax request experiencing 500 Internal Server Error. Uncertain about the source of the issue

I'm encountering a 500 Internal Server Error and I'm struggling to pinpoint the root cause of the issue. The purpose of this request is to delete a comment with a specific id from the database. The id is passed through a hidden input field. Below ...

Exploring table iteration in Angular 7

I am looking to create a table with one property per cell, but I want each row to contain 4 cells before moving on to the next row... This is what I want: <table> <tr> <td> <mat-checkbox>1</mat-checkbox& ...

Discovering if a page can be scrolled in Angular

Hey there, I recently started working with Angular and created an app using the angular material stepper. The issue I'm facing is that some of the steps have longer content, causing the page to become scrollable. I am now trying to find a way to deter ...

Dawn Break Alarm Timepiece - Online Platform

My buddy recently purchased a "sunrise alarm clock" that gradually brightens to mimic a sunrise and make waking up easier. I had the thought of replicating this effect with my laptop, but I've been facing issues with getting the JavaScript time funct ...

Tips on duplicating an object within a React state without using references

In my React application, I have a state that contains several objects. I need to make a copy of the c: "value" field from the initial state before it gets replaced by the input value from e.target.value. The purpose behind this is to ensure that ...

The functionality of MethodBase.IsConstructor may not behave as expected when dealing with a static constructor

Just a quick observation: The property MethodBase.IsConstructor does not seem to function properly with static constructors. Interestingly, this aspect is not mentioned in the documentation (which states: "true if this method is a constructor represented b ...

Leveraging jQuery event listeners within a Javascript class initialization

Recently delving into OOP in JavaScript, I've been revamping some of my old code to make it more reusable. Instead of leaving them as inline scripts, I decided to encapsulate them within JavaScript classes. Here is an example of one of my classes: ...

How can the datetime value of the Apex Charts datapoint be shown in the tooltip?

I'm struggling to find the proper location within the w.globals object to display the x-axis value, which is a datetime, in the tooltip of the datapoint. var chartOptions = { ... xaxis: { type: "datetime" }, tooltip: { x: { format: " ...

Can someone help clear up this confusion with CSS?

Why is image 6.png selected, when all the images are direct descendants of the div shape? Thank you for your assistance, it's greatly appreciated. As far as I know, it should select all the divs because they are all direct descendants of the div #shap ...

What is the best way to use a button to hide specific divs upon clicking?

Is there a way to use a button onclick event to hide specific divs within a parent div? I've tried using .toggleClass('.AddCSSClassHere') but I'm not sure how to apply it to multiple divs. The jQuery snippet provided only allows me to h ...

utilize javascript variables within an HTML document

I keep encountering a strange error (Express 400 Error: Bad Request) Some lines are translated to the variable value, while others just output an error. This is an example of my code: exports.add_comment = function(req, res){ var id = req.params.id; ...

Adding click functionality to dynamically generated list items in jQuery and HTML

I'm encountering an issue while trying to assign click events to dynamically added HTML elements in jQuery. Despite extensive research within this community, I find myself more confused than before. Below is the snippet of code causing me trouble: v ...

leveraging third party plugins to implement callbacks in TypeScript

When working with ajax calls in typical javascript, I have been using a specific pattern: myFunction() { var self = this; $.ajax({ // other options like url and stuff success: function () { self.someParsingFunction } } } In addition t ...

Selecting objects within a small three.js view

I am able to showcase an entire page filled with graphical elements using three.js and can even select objects by clicking on them. However, when attempting to display three.js graphics in a small viewport within an HTML page, issues arise. In order to ca ...

Incorporating a PHP file containing both PHP and JavaScript variables into an AJAX document

I'm facing an issue with my application that involves a language file called "lang.php", along with "index.php" and "ajax.php" files. The "lang.php" file contains both PHP and JavaScript variables which I include in both "index.php" and "ajax.php" to ...

Referring one sub-document to another sub-document in Mongoose

In my customer schema, there are two sub-document sets; orders and children, here is the structure: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const ObjectId = Schema.ObjectId; const childrenSchema = new Schema({ &a ...

Is it possible for two components to send two distinct props to a single component in a React application?

I recently encountered a scenario where I needed to pass a variable value to a Component that already has props for a different purpose. The challenge here is, can two separate components send different props to the same component? Alternatively, is it po ...

The shared hosting environment encountered an error during the Next JS build process

When I execute the command "npm run build" on my shared hosting server, it throws an error message: spawn ENOMEM. Interestingly, this command runs perfectly fine on my localhost and has been running smoothly on the hosting server for a few weeks until yest ...