自定义验证器会触发,但不能阻止回发 [英] Custom validator fires but does not prevent postback

查看:82
本文介绍了自定义验证器会触发,但不能阻止回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多与此有关的问题,但是我很沮丧!请帮忙!

I've seen a lot of questions about this already, but I'm stumped! Please help!

我有一个customvalidator。它正在触发,但是并不能阻止回发。请帮助我!我可以看到console.log在发布之前注册。但是,无论如何它都会回发。如何防止回发?

I have a customvalidator. It's firing but it's not preventing postback. Please help me in doing so! I can see that console.log registers before the post. But, it posts back anyway. How do I prevent the postback?

我尝试添加控件进行验证,并验证等于true的空文本。我还尝试添加e.preventdefault,该方法不起作用:(

I've tried adding a control to validate, and validate empty text equal to true. I also tried adding e.preventdefault, which did not work :(

如何防止回发?

    <script type="text/javascript">
//<![CDATA[
function validateWhyUnlikely(source, args) {
    console.log(1);
    args.isValid = false;
}
//]]>

<asp:TextBox ID="txtWhyUnlikely" runat="server" Rows="4" cols="20"
            CssClass="surveyTextArea" />
<asp:CustomValidator runat="server" ID="cfvWhyUnlikley" ErrorMessage="Please provide a reason since you rated an item as unlikely to provide."
        CssClass="surveyError surveySmallIndent" Display="Dynamic" 
        ClientValidationFunction="validateWhyUnlikely" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="smallSpecial" OnClick="btnSubmit_Click" />


jQuery(document).ready(function () {
    jQuery('#<%= btnSubmit.ClientID %>').click(function (e) {
        if (Page.IsValid == false) {
            console.log(false);
            e.preventDefault();
            return false;
        }
    });
});


推荐答案

虽然我不确定您为什么会这样,但一切看起来都不错将 Click 函数附加到提交按钮。我会删除它并对其进行测试,因为它可能会覆盖默认行为。

Everything looks ok althought I am not sure why you are attaching the Click function to your submit button. I would remove that and test it as it maybe be overriding the default behavior.

此外,我认为您需要将 IsValid 属性:

Also I think you need to capitalize the IsValid property:

args.IsValid = false;

这篇关于自定义验证器会触发,但不能阻止回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆