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

时间:2015-06-16 13:57:12

标签: c# asp.net validation validationsummary

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

<%@ 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”并显示

enter image description here

点击button2后,它会验证验证组“g2”并显示

enter image description here

嗯,但我不想在点击button2时松开验证摘要1(组g1)消息。

另外,我不想在点击事件上验证组g1和g2。

为什么我在button2点击事件上丢失了验证g1消息。

1 个答案:

答案 0 :(得分:0)

这是因为在任一点击事件中只发生一次验证。 您可能希望在两个点击事件中检查两个验证。 或者在Page reload事件中插入两者。