Demonstrates the CountTrueConditionsValidator
The CountTrueConditionsValidator lets you define a list of
Condition
objects that evaluate many fields.
Evaluates how many of those Conditions are true (evaluate as “success”)
and compares them to a minimum and maximum. The condition indicates success when
the count of true conditions is greater or equal to the minimum and less than or
equal to the maximum.
Here are some common uses for this Validator:
- Limit the number of marks amongst a list of checkboxes (not using a CheckBoxList).
Set the maximum to the limit. Use one CheckStateCondition for each checkbox.
- Require that at least one checkbox is marked in a column of checkboxes in a DataGrid.
Simply set the minimum to 1 and add CheckStateCondition objects programmatically
in the DataGrid’s ItemCreated event handler.
- Require that only one of several textboxes have text. Set the minimum and maximum
to 1. Use one RequiredTextCondition for each textbox.
Specify a list of
Conditions
in the Conditions
property. You can add any Condition class available within DES.
The Minimum and
Maximum properties determine the limits when they are not 0.
You can use
MultiCondition classes
to build Boolean expressions that represent a
single Condition in the collection. When the Boolean expression is evaluated as
true, the count is incremented. The child condition objects of the expression do
not contribute to the count.
The ErrorMessage and
SummaryErrorMessage property supports several tokens. “{COUNT}” is the
number of selected items. “{MINIMUM}” is replaced by the property Minimum. “{MAXIMUM}”
is replaced by the property Maximum.
Controls
Using a DataList with
Maximum=3. Select no more than 3 colors.
Select no more than 3. You have selected 0 colors.
Using a powerful shortcut
Instead of adding the ItemCreated event on a grid or list, you can instead assign
the ID of the grid or list to the
CountTrueConditionsValidator.ListControlID property.
Add one Condition object that will be used with each row to the
Conditions collection.
That object should have its
ControlIDToEvaluate property
assigned to the ID of the control in the row to evaluate.
DES will create a list based on that Condition object, one for each row.
This example is the same as above, except there is no ItemCreated event and the
CountTrueConditionsValidator.ListControlID property is assigned to the DataList's ID.
Select no more than 3. You have selected 0 colors.
Source Code (C#)
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList1.DataSource = CreateDataSource();
DataList1.DataBind();
DataList2.DataSource = CreateDataSource();
DataList2.DataBind();
}
}
protected ICollection CreateDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Color", typeof(System.Drawing.Color)));
DataRow dr = dt.NewRow();
dr[0] = System.Drawing.Color.Red;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = System.Drawing.Color.Green;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = System.Drawing.Color.Yellow;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = System.Drawing.Color.White;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = System.Drawing.Color.Blue;
dt.Rows.Add(dr);
DataView dv = new DataView(dt);
return dv;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (PeterBlum.DES.Globals.WebFormDirector.IsValid)
{
}
}
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
Control vCheckBox = e.Item.FindControl("CheckBox1");
if (vCheckBox != null)
CountTrueConditionsValidator1.Conditions.Add(new CheckStateCondition(
CountTrueConditionsValidator1, vCheckBox));
}
</script>
Using a DataList with <span class="PropertyName">Maximum</span>=3. Select no more than 3 colors.<br/><br/>
<asp:DataList ID="DataList1" runat="server" OnItemCreated="DataList1_ItemCreated">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Panel ID="Color" runat="server" BackColor='<%# Eval("Color")%>' Style="border:thin solid black; display:inline-block; width:20px;height:20px">
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<des:CountTrueConditionsValidator ID="CountTrueConditionsValidator1" runat="server"
ErrorMessage="Select no more than {MAXIMUM}. You have selected {COUNT} {COUNT:color:colors}."
Maximum="3" />
<br/>
<h2>Using a powerful shortcut</h2>
Instead of adding the ItemCreated event on a grid or list, you can instead assign
the ID of the grid or list to the <span class="PropertyName">CountTrueConditionsValidator.ListControlID</span> property.<br/>
Add one Condition object that will be used with each row to the <span class="PropertyName">Conditions</span> collection.
That object should have its <span class="PropertyName">ControlIDToEvaluate</span> property
assigned to the ID of the control in the row to evaluate.<br/>
DES will create a list based on that Condition object, one for each row.<br/>
This example is the same as above, except there is no ItemCreated event and the
<span class="PropertyName">CountTrueConditionsValidator.ListControlID</span> property is assigned to the DataList's ID.<br/><br/>
<asp:DataList ID="DataList2" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Panel ID="Color" runat="server" BackColor='<%# Eval("Color")%>' Style="border:thin solid black; display:inline-block; width:20px;height:20px">
</asp:Panel>
</ItemTemplate>
</asp:DataList>
<des:CountTrueConditionsValidator ID="CountTrueConditionsValidator2" runat="server"
ErrorMessage="Select no more than {MAXIMUM}. You have selected {COUNT} {COUNT:color:colors}."
Maximum="3" ListControlID="DataList2" >
<Conditions>
<des:CheckStateCondition ControlIDToEvaluate="CheckBox1" Checked="true" />
</Conditions>
</des:CountTrueConditionsValidator>
<br/>
<br/>
<des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click"></des:Button>