我怎么能有条件地阻止形式从服务器端/代码隐藏提交?(How can I conditionally

2019-10-24 07:43发布

当我的“提交”(保存)按钮被点击时,一件事情它的作用是检查两个“总计”丘壑匹配,并返回,如果他们不这样做,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);
    }
}

Answer 1:

首先Default.aspx的来源:

    <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" />

而对于Default.aspx.cs代码:

public partial class Default : System.Web.UI.Page
{

    protected void btnSave_Click(object sender, EventArgs e)
    {
        //TODO: do your stuff here...
    }

}


Answer 2:

如果你是在谈论防止形式在Web浏览器提交,你应该使用该客户端代码(即JavaScript的)。 即使有办法做到这一点在后面的代码,它只是被翻译成客户端代码。 否则,如果它保留编辑值的形式,甚至会提交。



文章来源: How can I conditionally prevent a form from submitting with server-side/code-behind?