ASP.NET YesNo Verification

In my code, I am attempting to display a Yes/No prompt on the client side after an if statement is executed. Depending on the user's choice, corresponding code should be triggered.

Here is what I have so far:

if (barcodes[t].ToString() == txtBarcode.Text)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>confirm('Item Already in Order, Continue?');</script;");
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Ok")
    {
       itemAdd();
       DBConnect.DisplayMessage(this, "Added to order");
    }
    else
    {
        DBConnect.DisplayMessage(this, "Not added");
        return;
    }
}

I am using a script in line three of the above code, and it displays correctly.

However, regardless of the user's choice, it always returns negative.

Thank you in advance :)

Answer №1

Looking for a straightforward solution? Check out this implementation from Mudassar Ahmed Khan's website:

ClientScriptPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientScriptPage.aspx.cs" Inherits="ClientScriptPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type = "text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");

            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";

            if (confirm("Do you want to save data?")) {
                confirm_value.value = "Yes";
            }
            else {
                confirm_value.value = "No";
            }

            document.forms[0].appendChild(confirm_value);
        }
    </script>

    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lblDisplayMessage" runat="server" Text="Click the button to display a confirm dialog."></asp:Label>
            <br /><br />
            <asp:Button ID="btnConfirm" runat="server" OnClick="OnConfirm" Text="Raise Confirm" OnClientClick="Confirm()"/>
        </div>
    </form>
</body>
</html>

ClientScriptPage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ClientScriptPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void OnConfirm(object sender, EventArgs e)
    {
        string confirmValue = Request.Form["confirm_value"];

        if (confirmValue == "Yes")
        {
            this.lblDisplayMessage.Text = "Added to order!";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
        }
        else
        {
            this.lblDisplayMessage.Text = "Not added to order!";
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
        }
    }
}

Results:

https://i.sstatic.net/e5U6p.gif

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

Error encountered: The module '@mui/x-data-grid' does not export 'GridActionsCellItem'

I'm facing an issue while trying to import 'GridActionsCellItem' from '@mui/x-data-grid'. Here's the code: import { GridActionsCellItem } from '@mui/x-data-grid'; An error message pops up indicating: Attempted impor ...

Expecting a declaration statement for exporting React

When attempting to export my component, I encounter an error in my editor stating export declaration statement expected Here is the code snippet: export Header from './Header/Header'; However, if I modify it like this: export {default as Head ...

How can I make text automatically resize within a fixed DIV based on the length of the text?

What's the best way to handle text in a fixed width and height DIV that can vary in length? I'm okay with reducing the size of the text when it overflows, but how can I make that happen? Any advice would be appreciated. Thanks! ...

Update the FontSize in the HTML dropdown menu using JavaScript

I am having trouble filling my Selection using a script. For example, when I try to populate my FontSizeMenu, I use the following code snippet: function FillFontSizeMenu() { FillSelection(GetPossibleFontSizes(), "fontSizeMenu"); } function GetPossib ...

The functionality of Express JS routers is experiencing issues

As I delved into learning nodejs and express, I decided to create a basic router. Initially, everything seemed to be working fine, but upon reopening the project, I encountered an issue. var express = require('express'); var app = express(); var ...

React Select is failing to display the selected value

I'm currently learning react-redux and encountering a couple of issues. In my code, I am using React Select Plus in the following manner: <Select id="portf" options={opts} onChange={value => portfolioSelector(value ...

The length of the array does not correspond to the index position

var index = 0; var length = ["a", "b", "c", "d", "e"].length; var interval = setInterval(function() { if (index < length) { console.log(index); console.log(length); index++; } else { clearInterval(interval); } }, 1000); Is there a solution t ...

Implementing and showcasing a validator directly from the back-end code

On my webpage, I have implemented various validators and a validation summary to ensure that required fields are filled out and that data input meets certain criteria. These validators are set up when the page is loaded and function based on regular expres ...

Guide on setting up background sound notifications with firebase-messaging-sw.js

const audio = new Audio(" "); Whenever I include this code in my firebase-messaging-sw.js file for the service worker, I keep encountering a reference error. Even though notifications come through when the browser is in the background, an Audio is not de ...

What methods can be employed to maintain a consistent width for a multi-line span that aligns with the content?

Inside a div, I have 2 spans. When the content of the first span is short enough to fit on one line, the span's width matches the text content and positions the second span next to it. If the content of the first span is long and causes it to wrap, t ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

Determine the sum of all cell values in rows that share the same date using JavaScript

I am currently developing a dashboard for an online food ordering platform. One of the essential functionalities I need to implement is the ability to display the total sales amount per day, categorized by payment methods, when users click on a specific mo ...

Is there a way to increase the value of an Int64 without utilizing a bigger data type?

After revising the code in this specific class to better suit my needs, I encountered a challenge during testing. The issue arose when attempting to convert this particular equation to utilize long inputs, as assigning values to the variables a and m resul ...

Removing Arcs from a Map in Leaflet

On my map, I use arc.js to draw trade routes between countries as arcs. Each arc represents a different year, and I want to be able to update them based on the selected year. This is how I create the arcs: // Draw arcs for each trade route var line_to_ad ...

The importance of variables in Express Routing

I'm really diving into the intricacies of Express.js routing concepts. Here's an example that I've been pondering over: const routes = require('./routes'); const user = require('./routes/user'); const app = express(); a ...

Ways to update the div's color according to a specific value

Below are the scripts and styles that were implemented: <script src="angular.min.js"></script> <style> .greater { color:#D7E3BF; background-color:#D7E3BF; } .less { color:#E5B9B5; background-co ...

Problem arises when attempting to slice an array that is defined in the parent component

Seems like a simple mistake, but here's what happened: In the main parent component, I have an array defined: ... <object-list-grid v-bind:objects="objectsList" ></object-list-grid> ... data() { return { ... ...

Forecast the width of an element using JavaScript

Is there a way to display tab elements on a webpage without them automatically wrapping into a new row if there are too many? Instead, I would like an arrow icon to appear at the end of the tabs so users can cycle between tab groups. The challenge is that ...

Node.js is experiencing difficulties loading the localhost webpage without displaying any error messages

I am having trouble getting my localhost node.js server to load in any browser. There are no errors, just a buffering symbol on the screen. The code works fine in VS Code. Here is what I have: server.js code: const http = require("http"); const ...

ASP.NET Core responds with a 404 error when an Angular request is made

I am currently working with ASP.NET Core in combination with Angular 2+. In my project, I have a Controller setup like this: public class ValuesController : Controller { [HttpGet] [Route("api/values/get")] [ResponseCache(NoStore = true, Lo ...