Show Modal Dialog / Confirmation Box based on user input in Code Behind / ASP.net
I have a grid view consisting of a textbox and a dropdown for each row in a grid view. I want a confirmation dialog to be displayed when the user enters a value into the textbox if it doesn't match the value of the corresponding label for each line where it is true.
Front end
<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>
What I found to work on my local machine when debugging was System.Windows.Forms.MessageBox.Show()
, but when I load the project into my IIS, no prompt appears.
Code for
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;
}
}
I understand because the dialog appears on the client side where the code is executed which is displayed on the server and not on the clients browser.
From reading other similar questions I also understand that I need to do this with JavaScript.
I am assuming that I will attach an event OnClick
to my update / submit button, but will javascript be able to iterate over row by row of my gridview, compare Label.text
and TextBox.Text
and set this background as textboxes color to yellow and display my dialog? And then it will know that it will continue executing the rest of the code after the user clicks OK?
source to share
First, in the frontend, I added JavaScript / jQuery which provided a different answer.
Front end
<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>
and in mine asp:Button
, which also runs the code
<asp:Button ID="btnUpdateClient" Text="Update Client" OnClientClick="confirmPayment()" OnClick="btnUpdateClientTable_Click" Visible="true" runat="server" />
Rear end
In my btnUpdateClientTable_Click () method
I need
string confirm = Request.Form["confirm_value"];
bool paymentErrorFlag = false;
to get a response to the confirmation dialog. This will contain a yes or no answer. The flag is set to true when a value is entered into the text box, but no value was selected from the dropdown list.
I would loop through each row in a grid view and use a combination paymentErrorFlag
and check for .Text == string.Empty
and .SelectedIndex == 0
to break the function and return;
without updating the database.
Essentially the key was Request.Form[]
returning the value of the selection in the confirmation dialog.
source to share
First of all, you cannot use Windows.Forms in asp.net application, remember that the UI runs in the browser of the client computer, the code you write runs on the server / strong>, on another computer ...
You have 2 options to achieve your goal:
-
Short cut, poor performance: add a javascript function to the end of the response, the javascript will display a confirmation box in the browser, but all the logic is still written in the code behind, this is done by calling ClientScript.RegisterClientScriptBlock
RegisterClientScriptBlock(this.GetType(), "confirmmsg", "<script> if(Confirm('do you want to proceed')) forms.submit();</script>");
-
Long and better way, javascript and jquery can easily implement the code you want, like this:
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 } } } }
source to share