Another benefit of using control templates is that you can pass parameter values to the control template and dynamically change content inside the template. In our example file Cities3-DynamicUpdates.mtd, we have added sort functionality to the previous example, Cities2-SwitchTemplates.mtd. It enables users to decide whether the display of countries and/or cities should be sorted (see screenshot below).
To implement the sort functionality, we did the following:
•Added an optional $sort parameter to both control templates.
•In the design of the top page, added a check box control on the right-hand side of each placeholder control (see screenshot below), and set the check box's column-width to wrap_content. We also set the Visible property of each check box appropriately.
•In the $PERSISTENT tree, added two new attributes, SortCountries and SortCities, and set the default fixed value of each to 0 (see screenshot below). We then set these two nodes to be updated by the respective check-box value (selected or not-selected); this was done by dragging each node on to its respective check box.
•For each placeholder, defined the value of the $sort parameter to be the XPath expressions, respectively, if ($PERSISTENT/Root/@SortCountries=1) then true() else false() and if ($PERSISTENT/Root/@SortCities=1) then true() else false().
•In each control template, the sequence that generates the repeating rows or repeating columns of the table is defined by the XPath expression if ($sort) then sort($values) else $values. In plain language, this means: If the value of the $sort parameter is true(), then sort the items in the $values sequence, otherwise use the $values sequence as it is (which is the order in the XML data file CitiesWorldwide.xml).
What happens is this:
•The values of the SortCountries and SortCities attributes are set to an initial value of 0. The values of both can be changed by selecting (value set to 1) or deselecting (value set to 0) their respective check boxes.
•The values of the respective attribute nodes (SortCountries and SortCities) are passed via the two placeholders to the $sort parameter of the respective control template as either true() or false().
•The XPath expression that selects the items of the $values sequence—which produces the repeating rows or repeating columns—either sorts the sequence or not, depending on the value of the template's $sort parameter (true() or false()).