Sunday, March 05, 2006
Trigger ValidationSummary popup?
A ValidationSummary control collects the ErrorMessage fields from all active validator objects in its group that have IsValid==false and displays them when it is triggered, typically by a Button. In many situations a ShowMessageBox popup message box is required.
However, when there is a CustomValidator using server-side validation in its group, it does not have the result from this validator available at postback, and it will not display a popup message for it at this time.
Upon redisplay of a page after postback, if the CustomValidator.IsValid was set to false at the server, then the message from that CustomValidator is displayed, but no popup message box is displayed by the ValidationSummary.
Is there a way to trigger the ValidationSummary control to display a popup message box after server-side validation?
However, when there is a CustomValidator using server-side validation in its group, it does not have the result from this validator available at postback, and it will not display a popup message for it at this time.
Upon redisplay of a page after postback, if the CustomValidator.IsValid was set to false at the server, then the message from that CustomValidator is displayed, but no popup message box is displayed by the ValidationSummary.
Is there a way to trigger the ValidationSummary control to display a popup message box after server-side validation?
Comments:
<< Home
There appears to be no solution to this issue provided by the Microsoft ASP.NET 2.0 framework. The ASP.NET 2.0 MSDN article, "ValidationSummary.EnableClientScript Property," may offer a clue. It says, "When this property is set to false...the ShowMessageBox property has no effect."
The obvious workaround is to insert JScript in the page. In an event handler function, first check whether the page is valid. If not, then client-side validation succeeded, but server-side validation failed.
If server-side validation failed, enumerate the validation controls and build JScript that will display the error messages from all the ones that failed. Put this into the page body, and perform no further actions. You will then get the popup message box that the ValidationSummary should have produced. (In the following, <= appears instead of < because of restrictions at this site.)
if (!IsValid) // server-side validation failed
{
StringBuilder oErrorScript = new StringBuilder();
string sScript1 = @"<=script type=""text/javascript"" language=""JavaScript"">";
string sScript2a = @" alert('";
string sScript2b = @"')";
string sScript3 = @"<=/script>";
oErrorScript.AppendLine(sScript1);
oErrorScript.Append(sScript2a);
ValidatorCollection oValidatorCollection = Validators;
foreach (BaseValidator oValidator in oValidatorCollection)
{
if (!oValidator.IsValid)
{
string sErrorMessage = oValidator.ErrorMessage + @"\n";
oErrorScript.Append(sErrorMessage);
}
}
oErrorScript.AppendLine(sScript2b);
oErrorScript.AppendLine(sScript3);
RegisterStartupScript("AlertMessage", oErrorScript.ToString());
return;
}
Post a Comment
The obvious workaround is to insert JScript in the page. In an event handler function, first check whether the page is valid. If not, then client-side validation succeeded, but server-side validation failed.
If server-side validation failed, enumerate the validation controls and build JScript that will display the error messages from all the ones that failed. Put this into the page body, and perform no further actions. You will then get the popup message box that the ValidationSummary should have produced. (In the following, <= appears instead of < because of restrictions at this site.)
if (!IsValid) // server-side validation failed
{
StringBuilder oErrorScript = new StringBuilder();
string sScript1 = @"<=script type=""text/javascript"" language=""JavaScript"">";
string sScript2a = @" alert('";
string sScript2b = @"')";
string sScript3 = @"<=/script>";
oErrorScript.AppendLine(sScript1);
oErrorScript.Append(sScript2a);
ValidatorCollection oValidatorCollection = Validators;
foreach (BaseValidator oValidator in oValidatorCollection)
{
if (!oValidator.IsValid)
{
string sErrorMessage = oValidator.ErrorMessage + @"\n";
oErrorScript.Append(sErrorMessage);
}
}
oErrorScript.AppendLine(sScript2b);
oErrorScript.AppendLine(sScript3);
RegisterStartupScript("AlertMessage", oErrorScript.ToString());
return;
}
Subscribe to Post Comments [Atom]
<< Home
Subscribe to Posts [Atom]