【问题标题】:YesNo Confirmation ASP.NETYesNo 确认 ASP.NET
【发布时间】:2018-05-31 18:25:32
【问题描述】:

我正在尝试在代码中的 if 语句之后获得一个 Yes No 提示以显示客户端,这取决于用户单击什么来执行代码。

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

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

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

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

提前致谢:)

【问题讨论】:

  • Web 开发不是这样工作的。整个服务器端将执行、渲染 HTML/CSS/JS 并将其发送到客户端。除非您向服务器发起另一个 HTTP 请求,例如单击链接、提交表单或 AJAX 调用,否则服务器端将在此时消失。您需要调整您的代码和思维方式以按照网络的方式工作。
  • 感谢石匠的输入,我将根据您的建议更改我的代码。
  • 您的里程可能会有所不同,而不是这个确切的场景,但是当您构建客户端到服务器的 AJAX 调用时,[WebMethod] 可能会有所帮助。在与数据库进行一些比较后,我更多地使用它从客户端获取数据以返回客户端。 stackoverflow.com/questions/19110170/…
  • spitballing,但我会让客户端是/否设置一个值,然后通过 WebMethod AJAX 调用将其推送回服务器。可能不会保证特定于 yes 的 AJAX 调用和特定于 no 的 AJAX 调用,但我不知道你盯着的代码库有多复杂。

标签: javascript c# asp.net


【解决方案1】:

这是来自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);
        }
    }
}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多