我还出现了重复的消息,并找到了以下对我有用的 javascript 解决方案。
Javascript:
<script language="javascript" type="text/javascript">
function RemoveValidationDuplicates(validationGroup) {
Page_ClientValidate(validationGroup);
if (typeof (Page_ValidationSummaries) == "undefined")
return;
var summary, sums, s;
for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
summary = Page_ValidationSummaries[sums];
summary.style.display = "none";
if (!Page_IsValid) {
if (summary.showsummary != "False") {
summary.style.display = "";
if (typeof (summary.displaymode) != "string") {
summary.displaymode = "BulletList";
}
switch (summary.displaymode) {
case "List":
headerSep = "<br/>";
first = "";
pre = "";
post = "<br/>";
final = "";
break;
case "BulletList":
default:
headerSep = "";
first = "<ul>";
pre = "<li>";
post = "</li>";
final = "</ul>";
break;
case "SingleParagraph":
headerSep = " ";
first = "";
pre = "";
post = " ";
final = "<br/>";
break;
}
s = "";
if (typeof (summary.headertext) == "string") {
s += summary.headertext + headerSep;
}
s += first;
for (i = 0; i < Page_Validators.length; i++) {
if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string") {
var tempstr = pre + Page_Validators[i].errormessage + post;
var isExist = s.search(tempstr);
if (isExist == -1)
s += pre + Page_Validators[i].errormessage + post;
}
}
s += final;
summary.innerHTML = s;
window.scrollTo(0, 0);
}
}
}
}
</script>
摘要和验证器共享一个 ValidationGroup,当提交按钮的 OnClientClick 被触发时,该 ValidationGroup 用作调用 RemoveValidationDuplicates 的输入:
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="validationGroupName"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox1" ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
<br/>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox2" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
<br/>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="TextBox4" ID="RequiredFieldValidator4" runat="server" ErrorMessage="RequiredFieldValidator1" ValidationGroup="validationGroupName"></asp:RequiredFieldValidator>
<br/>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="RemoveValidationDuplicates('validationGroupName')"/>
</div>
最初由 KIRAN 在csharpquery.blogspot.nl上发布的解决方案
3 月 23 日更新:更改了 javascript 函数以使其可重用,删除了一些冗余代码。