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);
}
}