-1

I am trying to get a Yes No Prompt to show client side after an if statement in code, and depending on what the user clicks to execute code.

So far I have the following:

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 envoke a script in the third line of the above code, which shows correctly.

However regardless of what the user chooses, it will always return negative.

Thanks in advance :)

Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
user3524014
  • 3
  • 2
  • 5
  • 2
    Web development doesn't work like that. The entire server side will execute, render HTML/CSS/JS and send that to the client. The server side goes away at that point unless you initiate another HTTP request to the server such as clicking a link, submitting a form, or an AJAX call. You need to adjust your code and mindset to work the way the web works. – mason May 31 '18 at 18:33
  • Thanks for the input mason, I am going to alter my code with the suggestion you have made. – user3524014 May 31 '18 at 18:45
  • Your mileage may vary, and not this exact scenario, but [WebMethod] may help when you're building out your client-to-server AJAX call. I've used it more for obtaining data from the client to get back to the client after some comparison against the database. https://stackoverflow.com/questions/19110170/how-to-call-webmethod-in-asp-net-c-sharp – Robert May 31 '18 at 19:18
  • spitballing, but I'd have the client-side yes/no set a value and then push it back to the server via the WebMethod AJAX call. Probably wouldn't warrant an AJAX call specific to yes and an AJAX call specific to no, but I don't know how complex of a code base you're staring down. – Robert May 31 '18 at 19:20

1 Answers1

1

Here's a nice and simple solution from Mudassar Ahmed Khan's website that fits your needs:

PageClientScript.aspx

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

<!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>

PageClientScript.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 PageClientScript : 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:

enter image description here

Máster
  • 981
  • 11
  • 23