Return to the menu   Select another DES Module

Demonstrates the IgnoreConditionValidator

The IgnoreConditionValidator is similar to the CustomValidator. Both Validators are designed for you to write your own code to determine if the Condition is valid or not. They go about it in different ways:

  • CustomValidator supports both client and server side validation. It requires your evaluation logic to be in its own event handler method.
  • IgnoreConditionValidator supports only server side validation. You usually use it when your validation code is part of some other logic that should not be separated out into the CustomValidator’s evaluation method. For example, you let your business layer run some validation against the data. If it’s good, that layer saves the data; if it’s bad, that layer returns an error message.

The IgnoreConditionValidator allows you to write code in one place and simply set the IsValid property to false on the Validator when an invalid condition is found.

  • You cannot create a client-side evaluation function. This is because it is designed for cases that are server specific, such as database queries and calling web services. However, the OverrideClientSideEvaluation property can provide client-side code to automatically hide the error message.
  • Generally you are writing a post back event handler for a button that determines something is invalid.
  • IsValid is always true after calling PeterBlum.DES.Globals.WebFormDirector.Validate(). To indicate the Validator is invalid, simply set IsValid to false.

Set the IsValid property to false on post back after detecting an error. Optionally set the ErrorMessage property to the desired message.

If there is a control on the page which it uses, set the RelatedControlID property to that control. RelatedControlID is supported by features like setting focus to the field with the error, changing the style of the field with the error, and ValidationSummary’s HyperLinkToFields property.


Controls

To see an error, enter anything except 'microsoft'. If you enter 'firefox', it customizes the error message.
Remember that a postback is always involved with this validator. So click Submit.




Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } } protected void TextBox1_TextChanged(object sender, EventArgs e) { if (TextBox1.Text.Trim() == "firefox") { IgnoreConditionValidator1.IsValid = false; IgnoreConditionValidator1.ErrorMessage = "Nice choice, but still enter 'microsoft'."; } else if (TextBox1.Text.Trim() != "microsoft") IgnoreConditionValidator1.IsValid = false; }
</script> To see an error, enter anything except '<i>microsoft</i>'. If you enter '<i>firefox</i>', it customizes the error message.<br/> Remember that a postback is always involved with this validator. So click Submit.<br/><br/> <des:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged"></des:TextBox> <des:IgnoreConditionValidator ID="IgnoreConditionValidator1" runat="server" ErrorMessage='Enter "microsoft"' RelatedControlID="TextBox1" /> <br/><br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

Return to the menu   Select another DES Module