简体   繁体   English

Page_ClientValidate多次验证。

[英]Page_ClientValidate is validating multiple times.

problem i have is that, the validation summary message(alert) is displayed twice. 问题是,验证摘要消息(警报)显示两次。 I cannot figure out the reason. 我无法弄清楚原因。

Please help. 请帮忙。 Here is the code 这是代码

function validate() //javascript function
{
    if (typeof(Page_ClientValidate) == 'function') 
    {
        var isPageValid = Page_ClientValidate();
        if(isPageValid)
        {
        }
    }
}

<asp:Button ID="btn1" runat="server" OnClientClick="validate()" Text="button"
                    ValidationGroup="ContactGroup" />

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" 
                    ShowMessageBox="true" ShowSummary="false" ValidationGroup="ContactGroup" />

The problem is that the function Page_ClientValidate takes an input parameter, if you don't specify the input then the validationsummary triggers once per groupname. 问题是函数Page_ClientValidate接受一个输入参数,如果你没有指定输入,那么validationsummary每个groupname触发一次。

In your case, the function triggers twice: once for groupname="ContactGroup" and another time for groupname="" 在您的情况下,该函数触发两次:一次用于groupname =“ContactGroup”,另一次用于groupname =“”

you should change 你应该改变

var isPageValid = Page_ClientValidate();

to

var isPageValid = Page_ClientValidate('');

if you don't want to specify a ValidationGroup, or if you want to specify a groupname then you need to call Page_ClientValidate like so: 如果您不想指定ValidationGroup,或者如果要指定组名,则需要像这样调用Page_ClientValidate:

var isPageValid = Page_ClientValidate('ContactGroup');

Hey there.First of all you should loose the ValidationGroup="ContactGroup" from the button because having validation group in it will first call the validation on the page then the OnClientClick event that contains the validate function which will call the page validation once again. 嘿那里。首先,您应该从按钮中松开ValidationGroup =“ContactGroup”,因为其中的验证组将首先调用页面上的验证,然后是包含validate函数的OnClientClick事件,该函数将再次调用页面验证。

Then you should pass the validation group "ContactGroup" to the Page_ClientValidate() function so it knows which controls to validate because simply calling Page_ClientValidate() will validate all controls regardless of their validation group(and it may display the validation message more than once, depending on how many validation groups you have). 然后,您应该将验证组“ContactGroup”传递给Page_ClientValidate()函数,以便它知道要验证哪些控件,因为只需调用Page_ClientValidate()将验证所有控件,而不管其验证组如何(并且它可能会多次显示验证消息,取决于您拥有的验证组数量)。

In short do something like this: 总之这样做:

function validate() //javascript function
{
    if (typeof(Page_ClientValidate) == 'function') 
    {
        var isPageValid = Page_ClientValidate('ContactGroup');
        if(isPageValid)
        {
          //your custom code
        }
    }
}    

<asp:textbox id="txtMyBox" runat="server"/>
<asp:requiredFieldValidator Id="rfv1" runat="server" ControlToValidate="txtMyBox"
ValidationGroup="ContactGroup" ErrorMessage="Bad!"/>

<asp:Button ID="btn1" runat="server" OnClientClick="validate()" Text="button"/>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" 
                    ShowMessageBox="true" ShowSummary="false" ValidationGroup="ContactGroup" />

just return false from the function and change the OnClientClick as shown below: 只需从函数返回false并更改OnClientClick,如下所示:

<asp:Button ID="btn1" runat="server" OnClientClick="return validate();" Text="button" 
                        ValidationGroup="ContactGroup" /> 

        function validate() //javascript function   
        {   
            if (typeof(Page_ClientValidate) == 'function')    
            {   
                var isPageValid = Page_ClientValidate();   
                if(isPageValid)   
                {   
                }   
            }   
        return false;

} 

You can make validation without show messages, use the following code segment,then use isPageValid variable: 您可以在没有显示消息的情况下进行验证,使用以下代码段,然后使用isPageValid变量:

 if (Page_ValidationSummaries && Page_ValidationSummaries[0] && Page_ValidationSummaries[0].showmessagebox) {
            var showMessagesOption = Page_ValidationSummaries[0].showmessagebox;
            Page_ValidationSummaries[0].showmessagebox = "False";
            isPageValid = Page_ClientValidate();
            Page_ValidationSummaries[0].showmessagebox = showMessagesOption;
        }

There is no need to manually call the Page_ClientValidate function, unless you're wanting to do the validation outside of a postback attempt. 除非您希望在回发尝试之外进行验证,否则无需手动调用Page_ClientValidate函数。

Set the buttons CausesValidation to true . 将按钮CausesValidation设置为true That'll run the validation. 那将进行验证。

删除按钮的单击事件,这会强制我认为第二次验证。

I know this is an old post, but here's a solution that may be more flexible. 我知道这是一个老帖子,但这里的解决方案可能更灵活。 Similar to other users suggestions, this solution accepts the validation group that is passed by default by the asp.net validation controls. 与其他用户建议类似,此解决方案接受asp.net验证控件默认传递的验证组。 This way you would not need to add the OnClientClick="validate()" on the Button control. 这样您就不需要在Button控件上添加OnClientClick="validate()"

//Make sure the Page_ClientValidate function exists
if (typeof (Page_ClientValidate) == "function") {
    //Stash the old implementation in a temp variable
    Page_ClientValidateOld = Page_ClientValidate;

    //Create a new implementation and store it
    //in Page_ClientValidate. Callers will now get
    //this implementation.
    Page_ClientValidate = function (validationGroup) {
        var isValid;

        //Call the old implementation first…
        isValid = Page_ClientValidateOld(validationGroup);

        //and then call our extension
        if (!isValid) {
            // Do something
        }

        return isValid;
    }
}

If you want to read more on this approach, I recommend that you look at this blog post: http://hyperthink.net/blog/interception-patterns-in-javascript/ 如果您想了解更多有关此方法的信息,我建议您查看此博客文章: http//hyperthink.net/blog/interception-patterns-in-javascript/

删除按钮的onclientclick事件没有必要

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM