如果文本框中的值无效,则阻止AsyncPostBackTrigger

时间:2016-08-09 14:54:11

标签: c# asp.net validation webforms

我的触发器中有以下代码:

import xml.etree.cElementTree as ET
import re

with open("index.xml") as f:
    xml = f.read()
tree = ET.fromstring(re.sub(r"(<\?xml[^>]+\?>)", r"\1<root>", xml) + "</root>")

而我的控制看起来像:

<asp:AsyncPostBackTrigger ControlID="InitPay" EventName="TextChanged" />

Jquery验证功能:

    <label class="ccsp">Initial Payment</label><br />
    <asp:TextBox ID="InitPay"  AutoPostBack="true" runat="server" OnTextChanged="InitPay_TextChanged" Style="width: 300px;">
    </asp:TextBox><b>$</b><br /> 
    <asp:CustomValidator ID="ValInitPay" ControlToValidate="InitPay" runat="server" ClientValidationFunction="Val_InitPay" OnServerValidate="ValInitPay_ServerValidate" ErrorMessage="Out of range error." Display="Dynamic"></asp:CustomValidator>

而验证功能背后的代码是:

function Val_InitPay(source, args) {
        if (Math.floor($('#InitPay').val()) == ($('#InitPay').val()) && $.isNumeric($('#InitPay').val())) {
            if ($('#InitPay').val() > ($("#InitPaymnet").val()) && $('#InitPay').val() < 1000)
            {
                args.IsValid = true;
            }
            else {
                args.IsValid = false;
            }
        }
        else {
            args.IsValid = false;
        }
    }

现在,当我在文本框中输入无效输入时,它会快速触发触发功能。它给出了错误但随后触发了触发器。有没有办法可以在值无效时停止触发?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果只在所有代码路径中使用jQuery的e.preventdefault()函数,我们确保输入无效,我们就可以停止服务器之旅。代码看起来像这样

function Val_InitPay(source, args) {
    if (Math.floor($('#InitPay').val()) == ($('#InitPay').val()) && $.isNumeric($('#InitPay').val())) {
        if ($('#InitPay').val() > ($("#InitPaymnet").val()) && $('#InitPay').val() < 1000)
        {
            args.IsValid = true;
        }
        else {
            args.IsValid = false;
            e.preventdefault()
        }
    }
    else {
        args.IsValid = false;
        e.preventdefault()
    }
}
相关问题