问题
When my "submit" (Save) button is clicked, one thing it does is verifies that the two "totals" vals match, and returns if they don't, fingerwagging the user with a red 'bad boy!' message:
message = new LiteralControl();
AddVerticalSpace();
Controls.Add(message);
decimal? paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if ((null == paymentTot) || (paymentTot < 0))
{
paymentTot = 0M;
}
decimal? grandTot = TryConvertToDecimal(boxGrandTotal.Text);
if ((null == grandTot) || (grandTot < 0))
{
grandTot = 0M;
}
if (grandTot != paymentTot)
{
AddVerticalSpace();
message.Text = "<span style='color:red'>Total and Payment Total do not match; Please enter the same amount for both values and try again.</span>";
return;
}
What I intend with the "return" is that the submit is aborted; yet, with the code above, the form is still submitted - it reverts to its initial appearance/state. How can I conditionally prevent this automatic submit behavior?
UPDATE
This is for Tyree Jackson:
Here's the server-side save button. First, it is conditionally dynamically created and configured thus:
if (AnyCheckboxSelected())
{
// Create Save button
this.Controls.Add(new LiteralControl("<br />"));
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save";
btnSave.Click += new EventHandler(btnSave_Click);
this.Controls.Add(btnSave);
AddVerticalSpace();
}
...and here is the event handler:
protected void btnSave_Click(object sender, EventArgs e)
{
LiteralControl message = null;
try
{
fullAddressChosen = rbFullAddress.Checked;
paymentToAnIndividualDropDownChosen = rbPaymentToIndividual.Checked;
paymentToAVendorDropDownChosen = rbPaymentToVendor.Checked;
message = new LiteralControl();
AddVerticalSpace();
Controls.Add(message);
decimal? paymentTot = TryConvertToDecimal(boxPaymentAmount.Text);
if ((null == paymentTot) || (paymentTot < 0))
{
paymentTot = 0M;
}
decimal? grandTot = TryConvertToDecimal(boxGrandTotal.Text);
if ((null == grandTot) || (grandTot < 0))
{
grandTot = 0M;
}
if (grandTot != paymentTot)
{
AddVerticalSpace();
message.Text = "<span style='color:red'>Total and Payment Total do not match; Please enter the same amount for both values and try again.</span>";
return;
}
ConditionallyCreateList();
SaveInputToList();
listOfListItems = ReadFromList();
message.Text = "Saving the data has been successful";
// Re-visiblize any rows with vals
if (RowContainsVals(3))
{
foapalrow3.Style["display"] = "table-row";
}
if (RowContainsVals(4))
{
foapalrow4.Style["display"] = "table-row";
}
if (RowContainsVals(5))
{
foapalrow5.Style["display"] = "table-row";
}
if (RowContainsVals(6))
{
foapalrow6.Style["display"] = "table-row";
}
AddVerticalSpace();
//CreatePDFGenButton();
GeneratePDFAndMsg();
btnGeneratePDF.Visible = true;
AddVerticalSpace();
GenerateLegalNotice();
}
catch (Exception ex)
{
message.Text = String.Format("Exception occurred: {0}", ex.Message);
}
}
回答1:
First Default.aspx source:
<script type="text/javascript" language="javascript">
function validateData() {
var payment = parseFloat(document.getElementById('boxPaymentAmount').value).toFixed(2);
var total = parseFloat(document.getElementById('boxGrandTotal').value).toFixed(2);
if (payment == null) payment = 0;
if (total == null) total = 0;
if (payment == total) {
return true;
} else {
alert('Total and Payment Total do not match. Please enter the same amount for both values and try again!');
return false;
}
}
</script>
<asp:TextBox ID="boxPaymentAmount" runat="server"></asp:TextBox><br />
<asp:TextBox ID="boxGrandTotal" runat="server"></asp:TextBox><br />
<asp:Button ID="btnSave" runat="server" Text="Button" OnClientClick="return validateData();" OnClick="btnSave_Click" />
And code for Default.aspx.cs:
public partial class Default : System.Web.UI.Page
{
protected void btnSave_Click(object sender, EventArgs e)
{
//TODO: do your stuff here...
}
}
回答2:
If you are talking about preventing a form from submitting in a web browser, you should use client side code (i.e. JavaScript) for that. Even if there was a way to do it in code behind, it would just be translated into client side code. Otherwise, the form will submit even if it retains the edited values.
来源:https://stackoverflow.com/questions/31707384/how-can-i-conditionally-prevent-a-form-from-submitting-with-server-side-code-beh