Aaron Li's Blog

Write it down before I forget

  Home  |   Contact  |   Syndication    |   Login
  30 Posts | 0 Stories | 21 Comments | 1 Trackbacks

News

Google

Archives

Other's Idea

Friday, May 11, 2007 #

Custom Validator Does Not Fire
     
I have code like this,
 
<asp:TextBox ID="txtEmailBody" Runat="server" />
<asp:CustomValidator ID="valEmailBody"
                           Runat="server"
                           ControlToValidate=" txtEmailBody "
                           Display="None"
                           OnServerValidate="Validate_EmailBody "
                           ErrorMessage="*" />
 
CustomValidator event does not fire if the validated control is empty.
 
If the validated control is a required field, it is easy to fix this problem by adding a RequiredFieldValidator control in addition to the CustomValidator. This means we have to use a RequiredFieldValidator rather than include the logic to checking the control not empty in the CustomValidator.
 
However, if the validated control is not always a required field, for example, in my case, in certain situation, the email body can be empty; but not in other situations. To make the CustomValidator work for this case, we can leave the ControlToValidate property of the custom validator blank. This will force the validator to fire on every round trip although it could somehow affect the performance, but at least the custom validator gets processed.