Return to the menu   Select another DES Module

Introduction to the CalculationController

The CalculationController lets you create a calculation involving IntegerTextBox, DecimalTextBox, CurrencyTextBox, and PercentTextBox controls. Its value can be displayed on the page and used by Validators and Conditions. The CalculationController has a powerful expression building tool. You can include constants, subexpressions, and IF statement logic based on Conditions. You can even provide your own custom functions for more advanced calculations.

The calculation is created within the Expression property on the CalculationController. This property is a collection which holds a list of the following objects (in the PeterBlum.DES.Web.WebControls namespace):

  • NumericTextBoxCalcItem - Identifies one IntegerTextBox, DecimalTextBox, CurrencyTextBox, or PercentTextBox that supplies a number into the calculation. Usually your calculation will have one or more of these. The TextBox will be set up so that any time it changes (after focus is lost), the calculation will run.
    Set the numeric TextBox in the TextBoxControlID property. Since a textbox can be blank or contain an illegal (non-numeric) value, use the BlankIsZero and InvalidIsZero properties to determine if these states use the value of 0 or indicate an error.
  • ConstantCalcItem - Supply a number constant into the calculation. Assign the constant to the Constant property.
  • ListConstantsCalcItem - Associate the items in a ListBox or DropDownList with constants. As the user changes the list’s selection, the calculation gets a different constant. For example, when SelectedIndex is 0, the value is 25 and when SelectedIndex is 1 through 4, the value is 30.
    Set the ListBox or DropDownList controls in the ListControlID property. Add PeterBlum.DES.Web.WebControls.ConstantForSelectedIndex objects to the ConstantsForSelectedIndexes property to define the selected indexes that are associated with a specific value.
  • CheckStateCalcItem - Associated two values with a CheckBox or RadioButton, where one value is used when its checked and the other when it isn’t. It can be used with CheckBoxList and RadioButtonList controls so long as you identify a specific button in the list by its index.
    Set the CheckBox, RadioButton, CheckBoxList, or RadioButtonList in the CheckedStateControlID property. When using a CheckBoxList or RadioButtonList, specify which button with its Index property. Define the values within the ValueWhenChecked and ValueWhenUnchecked properties.
  • ParenthesisCalcItem - Create a subexpression whose items are calculated together in its own Expression property. It’s the same idea as using parenthesis in computer code. For example, in (TextBox1 * 4) + (TextBox2 * 3) the Expression property contains two ParenthesisCalcItem objects, each which hold their own NumericTextBoxCalcItem and ConstantCalcItem objects.
  • ConditionCalcItem - Create an IF statement that uses any Condition object to determine whether to run subexpressions located in ExpressionWhenTrue and ExpressionWhenFalse properties. This allows great flexibility in your calculations. The Condition object is assigned to the Condition property.
  • CalcControllerCalcItem - Use another CalculationController for a part of the calculation. While you can reproduce a calculation several times in an expression, programmers prefer to write functions to encapsulate code that is reused.
    Set the ID of the other CalculationController in the ControlID property or a reference to CalculationController object in the ControlInstance property.
  • TotalingCalcItem - If you have to total a column in a ListView, GridView, DataList, or Repeater, a TotalingCalcItem object in the CalculationController's expression.
    See "TotalingCalcItem" for details and an example.

The result can be displayed in a DES numeric TextBox or Label. Set the ID of that control in the ShowValueControlID property or a reference to the control in the ShowValueInstance property.


Controls

NumericTextBoxCalcItem

IntegerTextBox1 + IntegerTextBox2

+ =

ConstantCalcItem

IntegerTextBox3 * 25

* 25 =

ListConstantsCalcItem

(value from the selected item in DropDownList1) * 25

* 25 =

CheckStateCalcItem

Suppose there are 3 radiobuttons grouped together and each has its own value. This determines the value of the selected one by evaluating all 3 and adding their values together. Those that are unchecked have a value of 0 (from their ValueWhenUnchecked property which defaults to 0.)


Result:

ParenthesisCalcItem

(DecimalTextBox1 + DecimalTextBox2) * 25

( + ) * 25 =

ConditionCalcItem

(IF (CheckBox1.Checked = True) THEN 10 ELSE 15) * 1.05

Price taxed at %5:

CalcControllerCalcItem

Uses the CalculationController from the first example, "NumericTextBoxCalcItem". Edit those values to see the Result updated. If their total exceeds 100, the value below is 0. Otherwise its the same as the first example.

IF (CalculationController2 is between 0 and 100) THEN (use CalculationController2’s value) ELSE (0)

Result:

Source Code (C#)

<h2>NumericTextBoxCalcItem</h2>
IntegerTextBox1 + IntegerTextBox2<br/><br/>
<des:IntegerTextBox ID="IntegerTextBox1" runat="server" /> + 
<des:IntegerTextBox ID="IntegerTextBox2" runat="server" /> =
<asp:Label ID="Result1" runat="server" />
<des:CalculationController id="CalculationController1" runat="server" ShowValueControlID="Result1" >
<Expression>
  <des:NumericTextBoxCalcItem TextBoxControlID="IntegerTextBox1" />
  <des:NumericTextBoxCalcItem TextBoxControlID="IntegerTextBox2" />
</Expression>
</des:CalculationController>
<br/>
<br/>
<h2>ConstantCalcItem</h2>
IntegerTextBox3 * 25<br/><br/>
<des:IntegerTextBox ID="IntegerTextBox3" runat="server" /> * 
25 =
<asp:Label ID="Result2" runat="server" />
<des:CalculationController id="CalculationController2" runat="server" ShowValueControlID="Result2" >
<Expression>
  <des:NumericTextBoxCalcItem TextBoxControlID="IntegerTextBox3" />
  <des:ConstantCalcItem Constant="25" Operator="Multiply" />
</Expression>
</des:CalculationController>
<br/><br/>
<h2>ListConstantsCalcItem</h2>
 (value from the selected item in DropDownList1) * 25<br/><br/>
<asp:DropDownList ID="DropDownList1" runat="server">
   <asp:ListItem Selected="True">Walking: 5</asp:ListItem>
   <asp:ListItem>Jogging: 10</asp:ListItem>
   <asp:ListItem>Singing: 10</asp:ListItem>
   <asp:ListItem>Climbing: 25</asp:ListItem>
</asp:DropDownList> * 25 = 
<asp:Label ID="Result3" runat="server" />
<des:CalculationController id="CalculationController3" runat="server" ShowValueControlID="Result3" >
<Expression>
  <des:ListConstantsCalcItem ListControlID="DropDownList1" >
    <ConstantsForSelectedIndexes>
       <des:ConstantForSelectedIndex StartIndex="0" Constant="5" />
       <des:ConstantForSelectedIndex StartIndex="1" EndIndex="2" Constant="10" />
       <des:ConstantForSelectedIndex StartIndex="3" Constant="25" />
    </ConstantsForSelectedIndexes>
  </des:ListConstantsCalcItem>
  <des:ConstantCalcItem Constant="25" Operator="Multiply" />
</Expression>
</des:CalculationController>
<br/>
<br/>
<h2>CheckStateCalcItem</h2>
Suppose there are 3 radiobuttons grouped together and each has its own value. This determines the value of the selected one by evaluating all 3 and adding their values together. Those that are unchecked have a value of 0 (from their ValueWhenUnchecked property which defaults to 0.)<br/><br/>
<asp:RadioButton ID="RadioButton1" runat="server" Text="Walking: 10" GroupName="G1" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="Jogging: 20" GroupName="G1"/>
<asp:RadioButton ID="RadioButton3" runat="server" Text="Climbing: 30" GroupName="G1"/>
<br/>
Result: <asp:Label ID="Result4" runat="server" />
<des:CalculationController id="CalculationController4" runat="server"   ShowValueControlID="Result4" >
<Expression>
  <des:CheckStateCalcItem CheckStateControlID="RadioButton1" ValueWhenChecked="10" />
  <des:CheckStateCalcItem CheckStateControlID="RadioButton2" ValueWhenChecked="20" />
  <des:CheckStateCalcItem CheckStateControlID="RadioButton3" ValueWhenChecked="30" />
</Expression>
</des:CalculationController>
<br/>
<br/>
<h2>ParenthesisCalcItem</h2>
 (DecimalTextBox1 + DecimalTextBox2) * 25<br/><br/>
(<des:DecimalTextBox ID="DecimalTextBox1" runat="server" /> + 
<des:DecimalTextBox ID="DecimalTextBox2" runat="server" />) * 25 =
<asp:Label ID="Result5" runat="server" />
<des:CalculationController id="CalculationController5" runat="server" ShowValueControlID="Result5">
<Expression>
  <des:ParenthesisCalcItem>
    <Expression>
      <des:NumericTextBoxCalcItem TextBoxControlID="DecimalTextBox1" />
      <des:NumericTextBoxCalcItem TextBoxControlID="DecimalTextBox2" />
    </Expression>
  </des:ParenthesisCalcItem>
  <des:ConstantCalcItem Constant="25" Operator="Multiply" />
</Expression>
</des:CalculationController>
<br/><br/>
<h2>ConditionCalcItem</h2>
(IF (CheckBox1.Checked = True) THEN 10 ELSE 15) * 1.05<br/><br/>
<asp:CheckBox ID="CheckBox1" runat="server" Text="Use the discount price of $10" />
Price taxed at %5:<asp:Label ID="Result6" runat="server" />
<des:CalculationController id="CalculationController6" runat="server" ShowValueControlID="Result6">
<Expression>
<des:ConditionCalcItem>
  <ConditionContainer>
    <des:CheckStateCondition ControlIDToEvaluate="CheckBox1" />
  </ConditionContainer>
  <ExpressionWhenTrue>
     <des:ConstantCalcItem Constant="10" />
  </ExpressionWhenTrue>
  <ExpressionWhenFalse>
     <des:ConstantCalcItem Constant="15" />
  </ExpressionWhenFalse>
</des:ConditionCalcItem>
<des:ConstantCalcItem Constant="1.05" Operator="Multiply" />
</Expression>
</des:CalculationController>
<br/><br/>
<h2>CalcControllerCalcItem</h2>
Uses the CalculationController from the first example, "NumericTextBoxCalcItem". Edit those values to see the Result updated.
If their total exceeds 100, the value below is 0. Otherwise its the same as the first example.<br/><br/>
IF (CalculationController2 is between 0 and 100) THEN
(use CalculationController2’s value) ELSE (0)<br/><br/>
Result: <asp:Label ID="Result7" runat="server" />
<des:CalculationController id="CalculationController7" runat="server" ShowValueControlID="Result7">
<Expression>
<des:ConditionCalcItem>
  <ConditionContainer>
    <des:RangeCondition ControlIDToEvaluate="CalculationController1" Minimum="0" Maximum="100" />
  </ConditionContainer>
  <ExpressionWhenTrue>
    <des:CalcControllerCalcItem ControlID="CalculationController1"/>
  </ExpressionWhenTrue>
  <ExpressionWhenFalse>
    <des:ConstantCalcItem Constant="0"/>
  </ExpressionWhenFalse>
</des:ConditionCalcItem>
</Expression>
</des:CalculationController>

Return to the menu   Select another DES Module