Thursday, January 7, 2010

In ValidationSummary remove duplicate ErrorMessage

< %@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" % >

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server" >
< title >Untitled Page< /title >
< script type="text/javascript" >
function test()
{
Page_ClientValidate('test');

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);
var val=document.getElementById('ValidationSummary1');
val.InnnerHTML=s;
}
}
}
}

< /script >
< /head >
< body >
< form id="form1" runat="server" >
< div >
< asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="test"/ >
< asp:TextBox ID="TextBox1" runat="server" >< /asp:TextBox >
< asp:RequiredFieldValidator ControlToValidate="TextBox1" ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="test" >< /asp:RequiredFieldValidator >
< br / >
< asp:TextBox ID="TextBox2" runat="server" >< /asp:TextBox >
< asp:RequiredFieldValidator ControlToValidate="TextBox2" ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="test" >< /asp:RequiredFieldValidator >
< br / >
< asp:TextBox ID="TextBox4" runat="server" >< /asp:TextBox >
< asp:RequiredFieldValidator ControlToValidate="TextBox4" ID="RequiredFieldValidator4" runat="server" ErrorMessage="RequiredFieldValidator1" ValidationGroup="test" >< /asp:RequiredFieldValidator >
< br / >
< asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="test()"/ >
< /div >
< /form >
< /body >
< /html >

1 comment:

Anonymous said...

Woww..i have been struggling since 3 hrs to eliminate duplicate error messages from validation summary. This worked like magic. Thanks to you.

Post a Comment

Post a Comment