I have managed to do this as follows:
1. I placed a validation summary control on the master page.
2. I created a class containing a dummy validator (code found on web somewhere - see below)
3. If your csla class is invalid, you can pass the broken rule collection to the following method (I placed all this code in a base page class). I also use ShowPageError() to display exceptions.
Works very well - the same validation summary on the master page can be used by both the client side validators and the csla server side validation.
********* base page code ****************
private ValidationSummary _customSummary = null; protected ValidationSummary CustomSummary{
get{
if (_customSummary == null){
_customSummary =
new ValidationSummary();}
return _customSummary;}
}
protected void ShowBrokenRules(BrokenRulesCollection brokenRulesCollection){
foreach (BrokenRule br in brokenRulesCollection){
ShowPageError(br.Description);
}
}
protected void ShowPageError(string errorDescription){
System.Web.UI.
Page pageRef = this.Page;CustomSummary.AddErrorMessage(errorDescription, pageRef);
}
********* Dummy Validator class****************
using
System;using
System.Data;using
System.Configuration;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;namespace
MyNamespace{
/// <summary> /// This control inherits the ASP.NET ValidationSummary control but adds /// the ability to dynamically add error messages without requiring /// validation controls. /// </summary> public class ValidationSummary : System.Web.UI.WebControls.ValidationSummary{
/// <summary> /// Allows the caller to place custom text messages inside the validation /// summary control /// </summary> /// <param name="msg">The message you want to appear in the summary</param> /// <param name="pageRef">The page where the message will appear</param> public void AddErrorMessage(string msg, System.Web.UI.Page pageRef){
System.Web.UI.
Page mypageRef = pageRef;mypageRef.Validators.Add(
new DummyValidator(msg));}
}
/// <summary> /// The validation summary control works by iterating over the Page.Validators /// collection and displaying the ErrorMessage property of each validator /// that return false for the IsValid() property. This class will act /// like all the other validators except it always is invalid and thus the /// ErrorMessage property will always be displayed. /// </summary> internal class DummyValidator : IValidator{
private string errorMsg; public DummyValidator(string msg){
errorMsg = msg;
}
public string ErrorMessage{
get{ return errorMsg;} set{ errorMsg = value;}}
public bool IsValid{
get{return false;} set{}}
public void Validate(){
}
}
}
Copyright (c) Marimer LLC