javascript - 在代码隐藏/ASP.net 中显示基于用户输入的模式对话框/确认框

标签 javascript c# jquery asp.net

我有一个 GridView ,其中包含一个文本框,并为 GridView 中的每一行提供下拉列表。 我希望在用户在文本框中输入值时显示一个确认对话框,如果该值与每行对应标签的值不匹配,且为真。

前端

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

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

来自阅读other similar questions我也明白我需要用 JavaScript 来实现这一点。
我想我会将 OnClick 事件附加到我的更新/提交按钮,但是 javascript 是否能够在我的 gridview 的行之间循环行,比较 Label.textTextBox.Text 并将文本框背景颜色设置为黄色并显示我的对话框?一旦用户单击“确定”,它会知道继续执行其余的代码隐藏吗?

最佳答案

首先,你不能在asp.net应用上使用Windows.Forms,记住UI是运行在客户端电脑的浏览器中,你写的后台代码是运行在服务器 端,在不同的计算机上...

您有 2 个选择来实现目标:

  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 - 在代码隐藏/ASP.net 中显示基于用户输入的模式对话框/确认框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31301683/

相关文章:

javascript - Asp.net/JQuery 在下载前显示/隐藏动画 gif

c# - LINQ让重复数据

php - $_POST 在 AJAX 调用的页面中不起作用

jQuery 数据表显示 NaN 条目中的 1 到 NaN

javascript - 如何使用phonegap在android中检测来电号码

javascript - 环回: configure acl and relations to list all objects belonging to logged used

c# - 我可以将 C# 扩展方法的可见性限制为同一程序集中的类吗?

jquery - 设为绝对:position div slide to left and fadeout - Bootstrap on WordPress

javascript - 如何在点击时禁用javascript以将表格导出到Excel?

c# - 如何存储 ASP.NET CommandName 和 CommandArgument 值?