Lennybacon says #1: Exceptions and ValidatorControls
I often get asked things through my messenger and today I decided to start sharing a few lines of the conversations…
Frank:
Is there a way to connect a Validator to an Exception so that the validation summary can be used to display the exception message?
Me:
Build a Custom Validator
Frank:
Ok
Me:
Use Page_Error or catch to set a “flag” to the Validator
Me:
Override the method EvaluateIsValid and return the state of the flag
Me:
This way the validator (if called on the postback) indicates its validation as true and after the flag is set false.
Me:
Here is some pseudo-code
try{
CriticalOperation();
} catch(MyException e) {
MyValidator.SetInvalid();
MyValidator.ErrorMessage = "bla bla: " + e.Message;
Page.Validate();
}
…
public class MyValidator
: CustomValidator{
bool _flag = true;
public void setInvalid(){
_flag=false;
}
public bool EvulateIsValid(){
return flag;
}
}