ASP的CustomValidator,推进出错后回发 [英] ASP CustomValidator, advancing to postback after error

查看:227
本文介绍了ASP的CustomValidator,推进出错后回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ASP验证(必填字段,普通防爆pression,...)加上额外的验证Java脚本功能的ASP .NET页面(例如,检查第二个日期比第一次约会大,中其他)。

I have an ASP .NET page with ASP validators (Required Field, Regular Expression,...) plus java script functions for additional validation (for example, to check if second date bigger than first date, among others).

我通常做的:

<script type="text/javascript">

    function validate() {
        // ...
        alert('Not valid!');
        return false;
    }    
</script>


<asp:Button ID="Button1" runat="server" Text="Add" 
            OnClientClick="return validate();" OnClick="Button1_Click" />

所以,按钮前进到回传如果ASP和JavaScript验证都通,并能正常工作。

So, the button advances to the postback if the asp and the javascript validation both pass, and it works fine.

我想出来的自定义验证:

I’m trying out the custom validator:

<asp:CustomValidator ID="CustomValidator1" 
EnableClientScript="true" runat="server" ControlToValidate="TextBox1" 
ClientValidationFunction="validate();" >
</asp:CustomValidator> 

(也试图与ClientValidationFunction =返回的validate();)

(also tried with ClientValidationFunction="return validate();")

但页面不断推进到回传,甚至呈现了无效警报之后...有什么想法?谢谢!

But the page is continuously advancing to the postback, even after showing the "not valid" alert... Any thoughts? Thanks!

推荐答案

在使用的CustomValidator,客户端验证函数需要接受参数参数。然后,以纪念为验证失败,您设置 arguments.IsValid 为false。这里是为的CustomValidator

When using a CustomValidator, the client side validation function needs to accept a source and an arguments parameter. Then, to mark the validation as failed, you set arguments.IsValid to false. Here is the MSDN page for the CustomValidator.

function validate(source, arguments) {
   // ...

   alert('Not valid!');
   arguments.IsValid=false;
}

这篇关于ASP的CustomValidator,推进出错后回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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