根据代码 Behind/ASP.net 中的用户输入显示模态 Dialog/Confirmation 框
Show Modal Dialog/Confirmation Box Based on User Input in Code Behind/ASP.net
我有一个网格视图,其中包含一个文本框和网格视图中每一行的下拉列表。 我希望在用户在文本框中输入值时显示确认对话框,如果该值与每行对应标签的值不匹配(其中为真)。
前端
<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 来实现这个。
我想我会在我的 update/submit 按钮上附加一个 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/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;
获取确认对话框的响应。这将包含 "yes" 或 "no" 响应。只要在文本框中输入值但未从我的下拉列表中选择值,该标志就会设置为 true。
我会循环遍历网格视图中的每一行并使用 paymentErrorFlag
的组合并检查 .Text == string.Empty
和 .SelectedIndex == 0
以破坏函数和 return;
不更新数据库。
本质上关键是 Request.Form[]
在确认对话框中返回选择的值。
我有一个网格视图,其中包含一个文本框和网格视图中每一行的下拉列表。 我希望在用户在文本框中输入值时显示确认对话框,如果该值与每行对应标签的值不匹配(其中为真)。
前端
<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 来实现这个。
我想我会在我的 update/submit 按钮上附加一个 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/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;
获取确认对话框的响应。这将包含 "yes" 或 "no" 响应。只要在文本框中输入值但未从我的下拉列表中选择值,该标志就会设置为 true。
我会循环遍历网格视图中的每一行并使用 paymentErrorFlag
的组合并检查 .Text == string.Empty
和 .SelectedIndex == 0
以破坏函数和 return;
不更新数据库。
本质上关键是 Request.Form[]
在确认对话框中返回选择的值。