自定义验证器会触发,但不能阻止回发 [英] Custom validator fires but does not prevent postback
问题描述
我已经看到很多与此有关的问题,但是我很沮丧!请帮忙!
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 $ c大写$ c>属性:
Also I think you need to capitalize the IsValid
property:
args.IsValid = false;
这篇关于自定义验证器会触发,但不能阻止回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!