是否确认ASP.NET

时间:2018-05-31 18:25:33

标签: javascript c# asp.net

我试图在代码中的if语句之后得到一个Yes No Prompt来显示客户端,并且取决于用户点击执行代码的内容。

到目前为止,我有以下内容:

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;
    }
}

我在上面代码的第三行中调用了一个脚本,该脚本正确显示。

但无论用户选择什么,它总是会返回负数。

提前致谢:)

1 个答案:

答案 0 :(得分:0)

这是Mudassar Ahmed Khan's website中适合您需求的简单明了的解决方案:

<强> 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);
        }
    }
}

<强>结果:

enter image description here