ASP.NET客户端页面验证失败后,我可以运行一些自定义脚本吗? [英] Can I run some custom script after ASP.NET client side page validation fails?

查看:99
本文介绍了ASP.NET客户端页面验证失败后,我可以运行一些自定义脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一些客户端脚本,如果并且仅在时,客户端页面验证失败,并且无法确定我可以将其挂钩的位置。

I'm trying to run some client side script if, and only if, the client side Page validation fails, and can't figure out where I can hook it in.

如果我将我的JavaScript函数绑定到提交表单的按钮的OnClientClick,它将在客户端验证之前运行。如果我将它绑定到表单的OnSubmit,只有在验证通过时才会触发。

If i bind my JavaScript function to the OnClientClick of the button that submits the form, it runs before the client side validation. If I bind it to the OnSubmit of the form, that only fires if the validation passes.

关于如何或在哪里挂钩这样的东西的任何想法?或者,如果您有其他建议,我会向他们开放。

Any ideas of how or where I can hook something like this up? Or if you have other suggestions, I'm open to them.

<form id="frm" runat="server" 
     onsubmit="FUNCTION HERE WONT FIRE IF VALIDATION FAILS">
<asp:requiredfieldvalidator id="vld" runat="server" controltovalidate="txt"/>
<asp:textbox id="txt" runat="server"></asp:textbox>

<asp:button id="cmd" runat="server" OnClick="dosomething"
     OnClientClick="FUNCTION FIRES BEFORE VALIDATION OCCURS">

</form>


推荐答案

尝试使用 Page_ClientValidate( )从JavaScript触发验证,然后你可以运行一些自定义代码:

Try using Page_ClientValidate("") to trigger validation from JavaScript, and then you can run some custom code:

validate = function(){
    var isValid = Page_ClientValidate(""); //parameter is the validation group - thanks @Jeff
    if (isValid){
        isValid = somethingToCheck();
    }
    return isValid;
}

<asp:Button ID="Button1" runat="server" CausesValidation="false" OnClientClick="return validate();" ... />

这篇关于ASP.NET客户端页面验证失败后,我可以运行一些自定义脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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