Demonstrates the MultiConditionValidator
The MultiConditionValidator evaluates multiple
Conditions
object within a Boolean
expression. Often, your validation rules cannot be expressed with one Condition.
MultiConditionValidator will handle many of those cases. This Validator is frequently
used. When you ask “how can I combine several Validators?”, choose the MultiConditionValidator.
NOTE: If there is another Validator that works for you, but needs to be disabled
at times, use that validator and set up the condition in its
Enabler
property.
Specify a list of Condition objects in the Conditions property. Each validator
has a companion Condition object, hosting only properties used for evaluating the
condition. For most validators, the condition class is the same name, replacing
"Validator" with "Condition". For example, RequiredTextValidator uses RequiredTextCondition.
Use the Operator to determine if the conditions are evaluated with AND or
OR logic. Use the NotCondition as a "NOT" operator.
One of the most useful Condition objects is MultiCondition, the condition defined
by the MultiConditionValidator itself. It allows creating subexpressions with its
own AND or OR operator.
Controls
Example 1
Shows nested MultiCondition objects allowing for both AND and OR operators in your logic.
(TextBox1 must have at least 10 chars) OR (TextBox 2 must be a decimal AND at least
two of the textboxes must have text)
This expression is not very realistic, especially since you can replace the
CountTrueConditions object
with
MultipleRequiredControlsCondition.
1
2
3
Fix it
Example 2
Enter a date within January of 2009, 2010, or 2011.
Select first month of 2009, 2010, or 2011
Example 3
If all contain "0", it is an error.
Another good validator for this case is the CountTrueConditionsValidator.
1
2
3
4
5
At least one textbox must not be 0
Source Code (C#)
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IntegerTextBox1.DataBind();
IntegerTextBox2.DataBind();
IntegerTextBox3.DataBind();
IntegerTextBox4.DataBind();
IntegerTextBox5.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PeterBlum.DES.Globals.WebFormDirector.IsValid)
{
}
}
</script>
<h2>Example 1</h2>
Shows nested MultiCondition objects allowing for both AND and OR operators in your logic.<br/>
(TextBox1 must have at least 10 chars) OR (TextBox 2 must be a decimal AND at least
two of the textboxes must have text)<br/>
This expression is not very realistic, especially since you can replace the <a href="CountTrueConditionsValidator.aspx" class="ControlClass">
CountTrueConditions</a> object
with <a href="MultipleRequiredControlsValidator.aspx" class="ControlClass">MultipleRequiredControlsCondition</a>.<br/><br/>
1<des:TextBox ID="TextBox1" runat="server"></des:TextBox>
2<des:TextBox ID="TextBox2" runat="server"></des:TextBox>
3<des:TextBox ID="TextBox3" runat="server"></des:TextBox>
<des:MultiConditionValidator ID="MultiConditionValidator1" runat="server"
ErrorMessage="Fix it" Operator="OR" >
<Conditions>
<des:TextLengthCondition ControlIDToEvaluate="TextBox1" Minimum="10" />
<des:MultiCondition Name="MultiCondition" Operator="AND">
<Conditions>
<des:RequiredTextCondition ControlIDToEvaluate="TextBox2" Name="RequiredTextCondition" />
<des:DataTypeCheckCondition ControlIDToEvaluate="TextBox2" DataType="Double" Name="DataTypeCheckCondition" />
<des:CountTrueConditions Maximum="1" Minimum="1" Name="CountTrueConditions">
<Conditions>
<des:RequiredTextCondition ControlIDToEvaluate="TextBox1" Name="RequiredTextCondition" />
<des:RequiredTextCondition ControlIDToEvaluate="TextBox3" Name="RequiredTextCondition" />
</Conditions>
</des:CountTrueConditions>
</Conditions>
</des:MultiCondition>
</Conditions>
</des:MultiConditionValidator>
<br/>
<br/>
<h2>Example 2</h2>
Enter a date within January of 2009, 2010, or 2011.<br/><br/>
<des:DateTextBox ID="DateTextBox1" runat="server" />
<des:MultiConditionValidator ID="MultiConditionsValidator2" runat="server" Operator="OR"
ErrorMessage="Select first month of 2009, 2010, or 2011">
<Conditions>
<des:RangeCondition ControlIDToEvaluate="DateTextBox1" Minimum="2009-01-01" Maximum="2009-01-31" />
<des:RangeCondition ControlIDToEvaluate="DateTextBox1" Minimum="2010-01-01" Maximum="2010-01-31" />
<des:RangeCondition ControlIDToEvaluate="DateTextBox1" Minimum="2011-01-01" Maximum="2011-01-31" />
</Conditions>
</des:MultiConditionValidator>
<br/>
<br/>
<h2>Example 3</h2>
If all contain "0", it is an error.<br/>
<i>Another good validator for this case is the <a href="CountTrueConditionsValidator.aspx" class="ControlClass">CountTrueConditionsValidator</a>.</i><br/><br/>
1
<des:IntegerTextBox ID="IntegerTextBox1" runat="server" IntegerValue="<%# 0%>"/><br/>
2
<des:IntegerTextBox ID="IntegerTextBox2" runat="server" IntegerValue="<%# 0%>"/><br/>
3
<des:IntegerTextBox ID="IntegerTextBox3" runat="server" IntegerValue="<%# 0%>"/><br/>
4
<des:IntegerTextBox ID="IntegerTextBox4" runat="server" IntegerValue="<%# 0%>"/><br/>
5
<des:IntegerTextBox ID="IntegerTextBox5" runat="server" IntegerValue="<%# 0%>"/><br/>
<des:MultiConditionValidator runat="server" ID="MultiConditionsValidator3"
Operator="OR"
ErrorMessage="At least one textbox must not be 0">
<Conditions>
<des:CompareToValueCondition ControlIDToEvaluate="IntegerTextBox1" ValueToCompare="1" Operator="GreaterThanEqual" />
<des:CompareToValueCondition ControlIDToEvaluate="IntegerTextBox2" ValueToCompare="1" Operator="GreaterThanEqual" />
<des:CompareToValueCondition ControlIDToEvaluate="IntegerTextBox3" ValueToCompare="1" Operator="GreaterThanEqual" />
<des:CompareToValueCondition ControlIDToEvaluate="IntegerTextBox4" ValueToCompare="1" Operator="GreaterThanEqual" />
<des:CompareToValueCondition ControlIDToEvaluate="IntegerTextBox5" ValueToCompare="1" Operator="GreaterThanEqual" />
</Conditions>
</des:MultiConditionValidator>
<des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>