当我的“提交”(保存)按钮被点击时,一件事情它的作用是检查两个“总计”丘壑匹配,并返回,如果他们不这样做,fingerwagging用红色用户“坏小子!” 信息:
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;
}
我打算与“回报”是,是提交中止; 然而,与上面的代码中,形式仍然提交 - 它恢复到其初始外观/状态。 我怎样才能有条件防止这种自动提交行为?
UPDATE
这是泰里·杰克逊:
这里的服务器端保存按钮。 首先,它有条件地动态创建并如此配置:
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();
}
...这里是事件处理程序:
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);
}
}