自定义(客户端)验证失败时的文本框仍会导致回发 [英] TextBox on failing Custom (Client) Validation Still causes Postback

查看:54
本文介绍了自定义(客户端)验证失败时的文本框仍会导致回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个文本框,其CausesValidation设置为True,在ServerSide Code中使用相应的事件处理程序启用了TextChange事件,并且在客户端JavaScript中也使用了CustomValidator代码

Hi,

I have a textbox who has CausesValidation set to True, TextChange event enabled with corresponding eventhandler in the ServerSide Code, and also a CustomValidator code in client Side JavaScript

<asp:textbox id="txtCustZipcode" size="10" tabindex="9" runat="server"

    MaxLength="10" AutoPostBack="true" OnTextChanged="txtCustZipcode_TextChanged"

    CausesValidation="True"></asp:textbox>
<asp:CustomValidator ID="cstValidate_txtCustZipCode" runat="server"

    ClientValidationFunction="fnzipcodeValidation" ControlToValidate="txtCustZipcode"

    SetFocusOnError="True"></asp:CustomValidator>











function fnzipcodeValidation(source, args)
{
    var strZipCodeValidator = /[&"#+>]/;
    args.IsValid=true;
    if(document.forms[0].txtCustZipcode.value!='')
        {
            if (strZipCodeValidator.test(trim(document.forms[0].txtCustZipcode.value)))
                {
                    alert('ZipCode cannot take &,#,+,> and double quotes, Pls correct');
                    document.forms[0].txtCustZipcode.focus()
                    document.forms[0].txtCustZipcode.select()
                    args.IsValid=false;
                    return false;
                }
        }
}



事实是,即使args.IsValid = false,也会发生服务器端TextChanged事件.

好的,我在服务器端EventHandler上尝试过此操作:



The thing is, even if the args.IsValid=false, the Server Side TextChanged event occurs.

Okay, I tried this on the Server Side EventHandler:

Protected Sub txtCustZipcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustZipcode.TextChanged
       cstValidate_txtCustZipCode.Validate()
  If cstValidate_txtCustZipCode.IsValid Then
'Do Something
  End If
End Sub



这可以正常工作,但是这种方法的麻烦在于:您是否注意到fnZipCodeValidation上的警报消息???几乎开了四次!

解决这个问题的任何聪明,简单的解决方案?



This works fine, but the trouble with this approach is: did you notice the alert message on the fnZipCodeValidation??? That fires almost four times!

Any clever, simple solution to over come this???

推荐答案

在加载事件中将这段代码

if(!ispostBack)
{
//将您的加载事件代码放在这里
}
in load event put this code

if(!ispostBack)
{
// put your load event code here
}
<pre lang="c#">


这篇关于自定义(客户端)验证失败时的文本框仍会导致回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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