即使通过自定义服务器端验证仍然触发ASP.NET按钮单击事件失败

时间:2021-08-26 15:53:36

I am having a problem where my button click event is still firing even though my custom server-side validation is set to args.IsValid = false. I am debugging through the code and the validation is definitely being fired before the button click, and args.IsValid is definitely being set to false once the custom validation takes place, but it always makes its way to the button click event afterwards. Any ideas on why this is?

即使我的自定义服务器端验证设置为args.IsValid = false,我的按钮单击事件仍在触发时出现问题。我正在通过代码进行调试,并且在按钮单击之前肯定会激活验证,并且一旦自定义验证发生,args.IsValid肯定被设置为false,但它总是在之后进入按钮单击事件。有关为什么会这样的想法?

2 个解决方案

#1


5  

Not sure 100% of the particulars, but to prevent code from continuing, add to your button click event handler:

不确定100%的细节,但为了防止代码继续,添加到您的按钮单击事件处理程序:

if (!Page.IsValid)
     return;

That will prevent the code from executing.

这将阻止代码执行。

#2


0  

On Client Side when OnClientClick="return SomeCustomClientCode();" is called, asp.net validators e.g required field validators are disabled and it does not gets listed in validators collection and does not validate the field validated by this validator and page post backs if custom validation passes...

在OnClientClick =“返回SomeCustomClientCode();”时在客户端被调用,asp.net验证器,例如必需的字段验证器被禁用,并且它不会在验证器集合中列出,并且如果自定义验证通过,则不验证由此验证器验证的字段和页面回发...

To avoid this explicitly enable asp.net validators in Custom validation code or else where so that it gets activated b4 page postback or in the begiining of custom validation as follows:

为了避免这种情况,请在自定义验证代码中明确启用asp.net验证器,否则它将被激活b4页面回发或开始自定义验证,如下所示:

ValidatorEnable(document.getElementById('<%=rfvDDLStatus.ClientID%>'), true);

rfvDDLStatus ==> required field validator which was not firing.

rfvDDLStatus ==>未触发的必填字段验证器。

ValidatorEnable ==> Client API to enable asp.net validator

ValidatorEnable ==>启用asp.net验证器的客户端API

#1


5  

Not sure 100% of the particulars, but to prevent code from continuing, add to your button click event handler:

不确定100%的细节,但为了防止代码继续,添加到您的按钮单击事件处理程序:

if (!Page.IsValid)
     return;

That will prevent the code from executing.

这将阻止代码执行。

#2


0  

On Client Side when OnClientClick="return SomeCustomClientCode();" is called, asp.net validators e.g required field validators are disabled and it does not gets listed in validators collection and does not validate the field validated by this validator and page post backs if custom validation passes...

在OnClientClick =“返回SomeCustomClientCode();”时在客户端被调用,asp.net验证器,例如必需的字段验证器被禁用,并且它不会在验证器集合中列出,并且如果自定义验证通过,则不验证由此验证器验证的字段和页面回发...

To avoid this explicitly enable asp.net validators in Custom validation code or else where so that it gets activated b4 page postback or in the begiining of custom validation as follows:

为了避免这种情况,请在自定义验证代码中明确启用asp.net验证器,否则它将被激活b4页面回发或开始自定义验证,如下所示:

ValidatorEnable(document.getElementById('<%=rfvDDLStatus.ClientID%>'), true);

rfvDDLStatus ==> required field validator which was not firing.

rfvDDLStatus ==>未触发的必填字段验证器。

ValidatorEnable ==> Client API to enable asp.net validator

ValidatorEnable ==>启用asp.net验证器的客户端API