Daniel Fisher (lennybacon.com)

SOA, DATA & THE WEB

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?

Lennybacon says:

1. build a Custom Validator

Frank:

Ok

Lennybacon says:

2. Use Page_Error or catch to set a "flag" to the Validator

Lennybacon says:

3. override the method EvaluateIsValid and return the state of the flag

Lennybacon says:

This way the validator (if called on the postback) indicates its validation as true and after the flag is set false.

Lennybacon says:

Here is some pseudo-code

Lennybacon says:

       try

       {

         CriticalOperation();

       }

       catch(MyException e)

       {

         MyValidator.SetInvalid();

         MyValidator.ErrorMessage = "bla bla: " + e.Message;

         Page.Validate();

       }

Lennybacon says:

       Validator : CustomValidator

       {

          bool flag = true;

          void setInvalid(){flag=false;}

          bool EvulateIsValid()

          {

               return flag;

          }

       }

 

http://www.staticdust.net/downloads/Web.ExceptionVisualizer.zip

Comments

Write a comment