验证摘要消息弹出两次

发布于 2024-11-08 06:57:54 字数 1394 浏览 2 评论 0原文

我的页面中有验证控件,并且我正在使用验证摘要消息框来显示验证消息,下面显示的 javascript 函数对我有用,但问题是我在按钮的 OnClientClick 事件上使用此 javascript 函数,当我单击带有表单控件的按钮未填充,它按预期显示验证摘要消息框,但是当我关闭摘要框时,它会再次显示,并且我需要关闭消息框才能消失,这意味着我每次都必须单击两次。有人可以纠正我我做错了什么吗?

这是我正在做的:-

 <asp:Button ID="SubmitButtonOne" runat="server" Text="Submit" OnClick="SubmitButtonOne_Click"
                         ValidationGroup="Cases" ClientIDMode="Static" OnClientClick="PleaseWaitShow()" />

这是 javascript 函数:

function PleaseWaitShow() {
                var isPageValid = true;
                // Do nothing if client validation is not active
                if (typeof (Page_Validators) != "undefined") {
                    if (typeof (Page_ClientValidate) == 'function') {
                        isPageValid = Page_ClientValidate();                       
                    } 
                } 
                if (isPageValid) {                  

                    document.getElementById('SubmitButtonOne').value = "Processing...";
                }            

                }

后面的代码:

protected void SubmitButtonOne_Click(object sender, EventArgs e)
    {       
        try
        {              
          // some functionality                   

        }

        catch (Exception)
        {
           //show errror message
        }

    }

I have validation controls in my page and I am using Validation summary message box to display the validation messages,the javascript function shown below worked for me but the problem is I am using this javascript function on OnClientClick event of the button and when I click the button with form controls not been filled its displaying the validation summary message box as expected but when i close the summary box it's getting displayed again and i need to close the message box to disappear, means i have to do two clicks everytime. Can somebody correct me what am I doing wrong.

Here is what I am doing:-

 <asp:Button ID="SubmitButtonOne" runat="server" Text="Submit" OnClick="SubmitButtonOne_Click"
                         ValidationGroup="Cases" ClientIDMode="Static" OnClientClick="PleaseWaitShow()" />

Here is the javascript function:

function PleaseWaitShow() {
                var isPageValid = true;
                // Do nothing if client validation is not active
                if (typeof (Page_Validators) != "undefined") {
                    if (typeof (Page_ClientValidate) == 'function') {
                        isPageValid = Page_ClientValidate();                       
                    } 
                } 
                if (isPageValid) {                  

                    document.getElementById('SubmitButtonOne').value = "Processing...";
                }            

                }

code behind:

protected void SubmitButtonOne_Click(object sender, EventArgs e)
    {       
        try
        {              
          // some functionality                   

        }

        catch (Exception)
        {
           //show errror message
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奶茶白久 2024-11-15 06:58:09

如果使用 Page_ClientValidate 方法,则应将 CauseValidation 设置为 false。 CausesValidation="false"

因为 Page_ClientValidate 已经对您验证的按钮或控件执行此操作

if you use Page_ClientValidate method, you should set causevalidation to false. CausesValidation="false"

because Page_ClientValidate is already doing this for button or control you validate

请持续率性 2024-11-15 06:58:09

请删除按钮的ValidationGroup。 ValidationSummary 的唯一用户
喜欢:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    
<asp:RequiredFieldValidator ID="rqvName" runat="server" ControlToValidate="txtName" ErrorMessage="Please enter Name" ValidationGroup="SaveName"> </asp:RequiredFieldValidator>
    
<asp:Button ID="btnSave" runat="server" OnClientClick="DisableButton();" Text="Save this Name" ></asp:Button>
    
 <asp:ValidationSummary ID="valSaveName" runat="server" ValidationGroup="SaveName" ShowMessageBox="true" ShowSummary="false" />

谢谢

Please remove ValidationGroup of Button. Only user for ValidationSummary
Like:

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    
<asp:RequiredFieldValidator ID="rqvName" runat="server" ControlToValidate="txtName" ErrorMessage="Please enter Name" ValidationGroup="SaveName"> </asp:RequiredFieldValidator>
    
<asp:Button ID="btnSave" runat="server" OnClientClick="DisableButton();" Text="Save this Name" ></asp:Button>
    
 <asp:ValidationSummary ID="valSaveName" runat="server" ValidationGroup="SaveName" ShowMessageBox="true" ShowSummary="false" />

Thanks

梦途 2024-11-15 06:58:07

不,这不是预期的行为。相反,这是 ASP.NET 验证 JS 代码中的错误。

ValidationSummaryOnSubmit 函数循环遍历所有摘要,并且对于每个摘要,循环遍历所有验证器。如果未指定组,则附加每个验证器的消息,导致相同消息重复 2(3、4、5?)次。

我通过修复函数解决了这个问题(盯着第 53 行左右):

....

for (i = 0; i < Page_Validators.length; i++)
{
    var validator = Page_Validators[i];

    if (validator.validationGroup != null && summary.validationGroup != null)
    {
        if (validator.validationGroup != summary.validationGroup) continue;
    }

    if (!validator.isvalid && typeof (validator.errormessage) == "string")
    {
        s += pre + validator.errormessage + post;
    }
}

....

No, it is not the expected behavior. Rather, it's a bug in ASP.NET validation JS code.

The ValidationSummaryOnSubmit function loops over all summaries, and for each summary, loops over all validators. If not group is specified, then each validator's message is appended, resulting in 2 (3, 4, 5?) repetitions of the same messages.

I solved the problem by fixing the function (staring at line 53 or so):

....

for (i = 0; i < Page_Validators.length; i++)
{
    var validator = Page_Validators[i];

    if (validator.validationGroup != null && summary.validationGroup != null)
    {
        if (validator.validationGroup != summary.validationGroup) continue;
    }

    if (!validator.isvalid && typeof (validator.errormessage) == "string")
    {
        s += pre + validator.errormessage + post;
    }
}

....
从来不烧饼 2024-11-15 06:58:04

这是预期的行为。

一旦 Page_ClientValidate 由于您在 PleaseWaitShow 方法内的显式调用而被触发,第二个是通过单击 PostBack 按钮进行隐式调用。

因此您会看到两次 ValidationSummary 消息框。

我没有解决方案来规避这个问题,但如果出现问题我会更新。

注意:我想指出的一件事是,由于您的提交按钮上有ValidationGroup="Cases",因此您应该将其传递给 Page_ClientValidate 方法。

更新1:我能想到的一种方法是尝试这样的事情:

1:OnClientClick =“return PleaseWaitShow();”

2:从PleaseWaitShow()返回isPageValid:

function PleaseWaitShow() {
            var isPageValid = true;
            ....
            ....
            ....
            return isPageValid;
        }

It is expected behavior.

Once Page_ClientValidate is fired due to your explicit call inside PleaseWaitShow method and second is an implicit call made by the PostBack button click.

And so you are seeing two times the ValidationSummary message box.

I don't have a solution to circumvent this but will update if something strikes.

Note: One thing I would like to point out is since you have ValidationGroup="Cases" on your submit button you should pass that to your Page_ClientValidate method.

Update 1: One way I can think of is trying something like this:

1: OnClientClick="return PleaseWaitShow();"

2: return isPageValid from PleaseWaitShow():

function PleaseWaitShow() {
            var isPageValid = true;
            ....
            ....
            ....
            return isPageValid;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文