在Code Behind/ASP.net中基于用户输入显示模态对话框/确认框

Show Modal Dialog/Confirmation Box Based on User Input in Code Behind/ASP.net

本文关键字:输入 用户 显示 对话框 确认 模态 ASP Behind Code net 于用户      更新时间:2023-09-26

我有一个网格视图,由一个文本框组成,网格视图中的每一行都有下拉列表。我想要一个确认对话框,当用户在文本框中输入一个值时,如果它不匹配每行对应标签的值,

前端

<asp:TemplateField HeaderText="Payment Amount">
    <ItemTemplate>
        <asp:Label ID="lblSuggestedAmount" runat="server"></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Payment Amount">
    <ItemTemplate>
        <asp:TextBox ID="txtbxActualAmount" Visible="true" runat="server"></asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="For Rental Month">
    <ItemTemplate>
        <asp:DropDownList ID="ddlPaymentMonth" Visible="true" runat="server"></asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

我发现在我的本地机器上调试时工作的是System.Windows.Forms.MessageBox.Show(),但当我将我的项目上传到我的IIS时,提示没有出现。

背后的代码
TextBox actualAmount = (TextBox)gvPayments.Rows[i].FindControl("txtbxActualAmount");
Label suggestedAmount = (Label)gvPayments.Rows[i].FindControl("lblSuggestedAmount");
if (Convert.ToDouble(actualAmount.Text) != Convert.ToDouble(suggestedAmount.Text)) {
    System.Windows.Forms.DialogResult dr = System.Windows.Forms.MessageBox.Show("Some payments have actual payment amounts greater than the suggested amount. Is this correct?", "Warning", 
        System.Windows.Forms.MessageBoxButtons.YesNo, 
        System.Windows.Forms.MessageBoxIcon.Warning,
        System.Windows.Forms.MessageBoxDefaultButton.Button1,
        System.Windows.Forms.MessageBoxOptions.ServiceNotification);
    if (dr == System.Windows.Forms.DialogResult.No) {
        return;
    } else {
        actualAmount.BackColor = Color.White;
    }
}

我理解,因为对话框显示在客户端,代码运行的地方,对话框显示在服务器上,而不是在客户端浏览器上。

从阅读其他类似的问题,我也明白我需要用JavaScript实现这一点。
我想我会将OnClick事件附加到我的更新/提交按钮,但javascript是否能够通过我的网格视图的行循环,比较Label.textTextBox.Text并将文本框背景颜色设置为黄色并显示我的对话框?然后,一旦用户点击ok,它会知道继续执行剩下的代码吗?

首先,你不能使用Windows。asp.net应用程序上的表单,记住UI是在客户端计算机的浏览器中运行的,你写的代码是在服务器端运行的,在不同的计算机上…

你有两个选择来实现目标:

  1. 短方式,性能差:添加javascript函数到响应的末尾,javascript将在浏览器中显示确认框,但所有的逻辑仍然写在代码后面,这是通过调用来完成的ClientScript。RegisterClientScriptBlock

    RegisterClientScriptBlock(this.GetType(),
        "confirmmsg",
        "<script> if(Confirm('do you want to proceed')) forms.submit();</script>");
    
  2. 长和最好的方式,javascript &Jquery可以很容易地实现您需要的代码,例如:

    function checkText(txt) {
        var allLables = $("[id*='lblSuggestedAmount']"); //get all the labels in the  grid
        for (var i = 0; i < allLables.length; i++) {
            if (allLabels[i].html() == txt) { 
                txt.style.bakground-color = "yellow";
                if(Confirm("This is message to confirm")) {
                    form1.submit() // what ever you need
                }
            }
        }
    }
    

首先,我在前端添加了一些JavaScript/jQuery,这是另一个答案提供的。

前端

<script>
// not sure exactly how much of this I need but here goes!
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
confirm_value.value = "no";
$(document).ready(function () {
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    confirm_value.value = "no";
});
function confirmPayment() {
    var allLabels = $("[id*='lblSuggestedAmount']");
    var allTextBoxes = $("[id*='txtbxActualAmount']");
    var failFlag = false;
    for (var i = 0; i < allLabels.length; i++) {
        if (allTextBoxes[i].value != "" && parseFloat(allLabels[i].innerText) != parseFloat(allTextBoxes[i].value)) {
            failFlag = true;
        }
    }
    if (failFlag) {
        if (confirm("Some payments have actual payment amounts that are different from the suggested amount. If this is correct, click Ok, if not click Cancel.")) {
            confirm_value.value = "yes";
            document.forms[0].appendChild(confirm_value);
        } else {
            confirm_value.value = "no";
            document.forms[0].appendChild(confirm_value);
        }
    }
}
</script>

和在我的asp:Button中也会触发代码隐藏

<asp:Button ID="btnUpdateClient" Text="Update Client" OnClientClick="confirmPayment()" OnClick="btnUpdateClientTable_Click" Visible="true" runat="server" />

后端

在btnUpdateClientTable_Click()方法

我需要的

string confirm = Request.Form["confirm_value"];
bool paymentErrorFlag = false;

获取对确认对话框的响应。这将容纳一个"是"或"否"的回应。当在文本框中输入了一个值,但没有从下拉列表中选择值时,该标志将被设置为true。

我会循环遍历网格视图中的每一行,并使用paymentErrorFlag.Text == string.Empty.SelectedIndex == 0的检查组合来打破函数和return;,而不更新数据库。

关键是在确认对话框中返回选择值的Request.Form[]