我有一个 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.text
和 TextBox.Text
并将文本框背景颜色设置为黄色并显示我的对话框?一旦用户单击“确定”,它会知道继续执行其余的代码隐藏吗?
最佳答案
首先,你不能在asp.net应用上使用Windows.Forms,记住UI是运行在客户端电脑的浏览器中,你写的后台代码是运行在服务器 端,在不同的计算机上...
您有 2 个选择来实现目标:
捷径,性能不佳:在响应的末尾添加 javascript 函数,javascript 将在浏览器中显示确认框,但所有逻辑仍然写在代码后面,这是通过调用来完成的 ClientScript.RegisterClientScriptBlock
RegisterClientScriptBlock(this.GetType(), "confirmmsg", "<script> if(Confirm('do you want to proceed')) forms.submit();</script>");
长而最好的方法,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/