Show Modal Dialog/confirmation Box Based On User Input In Code Behind/asp.net
Solution 1:
First of all, you can't use Windows.Forms on asp.net application, remember that the UI is running in the browser of the client computer, the code behind that you write is running on the server side, in different computer...
You have 2 options to achieve the goal:
Short way, bad performance: Add javascript function to the end of the response, the javascript will show confirmation box in the browser, but all the logic still written in code behind, this is done by calling to ClientScript.RegisterClientScriptBlock
RegisterClientScriptBlock(this.GetType(), "confirmmsg", "<script> if(Confirm('do you want to proceed')) forms.submit();</script>");
Long and best way, javascript & jquery can easily implement the code you need, for example:
functioncheckText(txt) { var allLables = $("[id*='lblSuggestedAmount']"); //get all the labels in the gridfor (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 } } } }
Solution 2:
Firstly in the front end I did add some JavaScript/jQuery that the other answer provided.
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";
});
functionconfirmPayment() {
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 my asp:Button
that also fires the code-behind
<asp:ButtonID="btnUpdateClient"Text="Update Client"OnClientClick="confirmPayment()"OnClick="btnUpdateClientTable_Click"Visible="true"runat="server" />
Back End
In my btnUpdateClientTable_Click() method
I needed
string confirm = Request.Form["confirm_value"];
bool paymentErrorFlag = false;
to get the response to the confirm dialog. This would house a "yes" or "no" response. The flag would get set to true whenever a value was input in the text box but a value was not selected from my drop down list.
I would cycle through every row in the grid view and using a combination of the paymentErrorFlag
and checks on .Text == string.Empty
and .SelectedIndex == 0
to break the function and return;
without updating the database.
Essentially the key was the Request.Form[]
returning the value of the selection in the confirmation dialog.
Post a Comment for "Show Modal Dialog/confirmation Box Based On User Input In Code Behind/asp.net"