我有一个 ValidationSummary 控件,它最初在开始时与我的所有验证控件一起工作,但是,后来当我将验证控件分配给组时,即在ValidationGroups
属性中给它们一个组名时,它似乎没有显示错误在服务器端验证失败的验证控件的消息。我正在使用调用来Page.Validate()
验证服务器端的所有控件。有没有人遇到过这个问题?
我对一个简单的页面进行了一些实验,并注意到当您将 CustomerValidator 分配给一个组时会发生这种情况,但没有该组,问题就会消失。
这是 Start.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Start.aspx.cs" Inherits="WebApplication3.Start" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation ="true" OnClick="Button1_Click" style="height: 26px"/>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ValidationGroup="Group1" Display="None" ErrorMessage="RequiredFieldValidator1"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ValidationGroup="Group2" Display="None" ErrorMessage="RequiredFieldValidator2"></asp:RequiredFieldValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox2" Display="None" ValidationGroup="Group2" ErrorMessage="Message for display in validation summary" OnServerValidate="cusCustom_ServerValidate"></asp:CustomValidator>
</div>
</form>
</body>
</html>
这是 Start.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class Start : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Page.Validate();
}
}
}