同一页面问题上的多个验证摘要控件,消失的验证摘要消息

multiple validation summary controls on same page issue, disappearing validation summary msg

我在一页上有两个验证摘要控件。两者都有不同的验证组。请看下面的代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx.cs" Inherits="TestApp.WebForm11" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="g1"/>
            <asp:Label ID="Label1" runat="server" Text="label1"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" Text="A"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ValidationGroup="g1" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="g1" CausesValidation="false" OnClick="Button1_Click" />
            <br />
            <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="g2"/>
            <asp:Label ID="Label2" runat="server" Text="label1"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" Text="B"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator2" runat="server" ValidationGroup="g2" OnServerValidate="CustomValidator2_ServerValidate">*</asp:CustomValidator>
            <br />
            <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="g2" CausesValidation="false" OnClick="Button2_Click" />
            <br />
        </div>
    </form>
</body>
</html>

代码隐藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestApp
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int num;
            if(!(int.TryParse(TextBox1.Text, out num)))
            {
                CustomValidator1.ErrorMessage = "cv1 msg";
                args.IsValid = false;
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int num;
            if (!(int.TryParse(TextBox2.Text, out num)))
            {
                CustomValidator2.ErrorMessage = "cv2 msg";
                args.IsValid = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Page.Validate("g1");
            if(this.Page.IsValid)
                Response.Write("button1 success");
            else
                Response.Write("button1 falied");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Page.Validate("g2");
            if (this.Page.IsValid)
                Response.Write("button2 success");
            else
                Response.Write("button2 falied");
        }
    }
}

现在,当我点击 button1 时,它会验证验证组 "g1" 并显示

并且当点击 button2 时,它验证验证组 "g2" 并显示

hmmmmmmm,但我不想在单击按钮 2 时丢失验证摘要 1(组 g1)消息。

此外,我不想在单击事件时同时验证组 g1 和 g2。

为什么我在按钮 2 单击事件上丢失验证 g1 消息。

这是因为在任一点击事件中只发生一次验证。 您可能想要检查两个点击事件中的两个验证。 或者在页面重新加载事件中插入两者。