自定义验证器onServerValidate方法不会影响Page_ClientValidate()结果 [英] Custom Validator onServerValidate Method does not affect Page_ClientValidate() result

查看:93
本文介绍了自定义验证器onServerValidate方法不会影响Page_ClientValidate()结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我在一天的时间里一直在研究这个问题并尝试了很多但是成功了。



我有具有OnServerValidate方法的自定义验证器,它与其他必需的字段验证器一起属于验证组。

我的提交按钮调用JavaScript函数来检查页面上的验证是否完整且是否有效,扩展下一个手风琴窗格(这可以使用客户端脚本完成)。



我面临的问题是,以下Javascript方法返回



即使服务器验证方法返回,Page_IsValid为true,Args.IsValid = False。



怎么可能验证组内的验证被忽略?



这是代码。

txtCity和txtPostalCode验证需要在这里完成。 />
邮政编码验证需要通过数据库查询在服务器上完成。



Hi guys,
I have been working on this problem over a quarter day and tried many things but succeed.

I have a custom validator that has an OnServerValidate method and it belongs to a validation group along with other required field validators.
My submit button calls a JavaScript function to check if the validations on the page are complete and if valid, expand the next Accordion Pane(this can be done using client side script).

The problem i am facing is that, the following Javascript method returns

Page_IsValid to true even though the Server Validation method returns, Args.IsValid=False.

How is that possible that a validation inside a validation group is ignored?

Here is the code.
txtCity and txtPostalCode validations needs to be done here.
Postal code validation needs to be done on the server via database query.

<asp:TextBox ID="txtCity" runat="server" Height="19px" Width="250px" MaxLength="25"></asp:TextBox>
                                     <asp:RequiredFieldValidator ID="ReqtxtSenderCity" runat="server"  validationgroup="PersonalInfoGroup"

                                    ControlToValidate="txtCity" Display="Dynamic" ErrorMessage="*"

                                     ></asp:RequiredFieldValidator>







<asp:TextBox ID="txtPostalCode" runat="server" Height="18px" Width="250px"></asp:TextBox>


                           <asp:CustomValidator id="CustomValidator1" runat="server"

                           ControlToValidate="txtPostalCode"

                           ValidateEmptyText="true"

                           OnServerValidate="ZipStateValidate"  validationgroup="PersonalInfoGroup"

                           ErrorMessage="*Zip Code is invalid" dispay="dynamic">
                           </asp:CustomValidator>





这是服务器方法







Here is the server Method


protected void ZipStateValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{

    if (ddlState.SelectedValue.ToString().Length > 0)
    {

        int intProvinceCode;
        string strZip = string.Empty;
        intProvinceCode = Convert.ToInt16(ddlState.SelectedValue);
        strZip = args.Value.ToString();
        BusinessLayer obj = new BusinessLayer();
        DataSet IsValidZip = obj.IsValidZIP(intProvinceCode, strZip);
        int isValid = 0;
        foreach (DataRow dr1 in IsValidZip.Tables[0].Rows)
        {

            isValid = Convert.ToInt16(dr1[0].ToString());

        }


        if (isValid == 1)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }

    }
}



这是JavaScript方法




Here is the JavaScript Method

// expand given accordion pane
                           function changeSelected(idx) {



                               if (Page_ClientValidate('PersonalInfoGroup')) {
                                  var ExtenderName = "<% =Accordion1.ClientID %>" + "_AccordionExtender"
                               var accordion = $find(ExtenderName);
                               accordion.set_SelectedIndex(idx);

                               }
                               else {
                                   return Page_IsValid;
                               }


                           }







这里是启动验证的按钮。

(在一个网站上建议使用Window.setTimeout,这就是我使用它的原因,但它对执行没有任何影响)






Here is the button that initiates the validation.
(Window.setTimeout was suggested at one website, that's why I am using it but it does not have any effect on the execution)

<asp:Button id="btnValidate" Autopostback="true" OnClick ="btnValidate_click" CausesValidation="true" OnClientClick= "window.setTimeout(function(){ changeSelected(1); },0);"  validationgroup="PersonalInfoGroup" runat="server" Text="Next"></asp:Button>







点击按钮后,

)当txtCity有一个值并且当txtPostalCode具有无效值时,Page_ClientValidate('PersonalInfoGroup')方法返回true并且Accordion Pane扩展,

2)在手风琴窗格展开后,ServerSide验证完成后,我从自定义验证器中看到验证错误。验证有效,但不影响Page_ClientValidate。



我的问题是,是否可以在java脚本方法之前在同一验证组中运行全部验证执行。在这种情况下,自定义验证器似乎执行得很晚。



如果没有,你有什么建议吗?

谢谢




As a result of the button click,
1) When txtCity has a value and when txtPostalCode has an invalid value, the Page_ClientValidate('PersonalInfoGroup') method returns true and the Accordion Pane expands,
2) After the accordion pane expands, the ServerSide validation gets completed and I see the validation errors from my custom validator. The validation works but does not affect the Page_ClientValidate.

My question is, Is it possible to run "all" the validations in the same validation group before the java script method executes. In this case, custom validator seems to be executed late.

If not, do you have any suggestions?
Thanks

推荐答案

find(ExtenderName);
accordion.set_SelectedIndex(idx);

}
else {
return Page_IsValid;
}


}
find(ExtenderName); accordion.set_SelectedIndex(idx); } else { return Page_IsValid; } }







这里是启动验证的按钮。

(在一个网站上建议使用Window.setTimeout,这就是我使用它的原因,但它对执行没有任何影响)






Here is the button that initiates the validation.
(Window.setTimeout was suggested at one website, that's why I am using it but it does not have any effect on the execution)

<asp:Button id="btnValidate" Autopostback="true" OnClick ="btnValidate_click" CausesValidation="true" OnClientClick= "window.setTimeout(function(){ changeSelected(1); },0);"  validationgroup="PersonalInfoGroup" runat="server" Text="Next"></asp:Button>







点击按钮后,

)当txtCity有一个值并且当txtPostalCode具有无效值时,Page_ClientValidate('PersonalInfoGroup')方法返回true并且Accordion Pane扩展,

2)在手风琴窗格展开后,ServerSide验证完成后,我从自定义验证器中看到验证错误。验证有效,但不影响Page_ClientValidate。



我的问题是,是否可以在java脚本方法之前在同一验证组中运行全部验证执行。在这种情况下,自定义验证器似乎执行得很晚。



如果没有,你有什么建议吗?

谢谢




As a result of the button click,
1) When txtCity has a value and when txtPostalCode has an invalid value, the Page_ClientValidate('PersonalInfoGroup') method returns true and the Accordion Pane expands,
2) After the accordion pane expands, the ServerSide validation gets completed and I see the validation errors from my custom validator. The validation works but does not affect the Page_ClientValidate.

My question is, Is it possible to run "all" the validations in the same validation group before the java script method executes. In this case, custom validator seems to be executed late.

If not, do you have any suggestions?
Thanks


这篇关于自定义验证器onServerValidate方法不会影响Page_ClientValidate()结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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