Home > Products > BPC PowerForms - Silverlight > Knowledge Base > Scripting and Formulas > Script Example : Setting the datasource of controls through script

Script Example : Setting the datasource of controls through script

In order to have filtered dropdowns, the most common approach is to use the Constraint feature of PowerForms which enables automatic filtering of a dropdown control (see this link for details)  

There are cases though when you want to manually apply the datasource of a control using business rules.

For example a set of COUNTRY and STATE combos.

The State combo needs to show available states when USA is selected in the COUNTRY control and the "Not Applicable" value otherwise.

This can be easily done through script. The COUNTRY control should be bound to the Country Lookup of the list whereas the STATE control should be a simple text field (NOT bound to a lookup/list since we want to set the datasource ourselves)

In the ValueChange event of the COUNTRY control add the following script :

from System import Array
target = template.GetControl("c_State")
country= control.GetValue()
a1 = Array[str](["AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL"])
a2 = Array[str](["Not Applicable"])
if country == "USA" :
else :

Script explained :

In order to change the datasource of a combobox control,  the target.InputControl.SetDataSource function should be used. It accepts an array of texts.

First we import the Array class from the the framework (from System import Array)

Then we get a reference for the STATE control (target = template.GetControl("c_State"))

We get the value of the country control (country= control.GetValue()

We create the data source lists we want to use (a1, a2)

We check the value of the country control and apply the datasource accordingly

As a last step, we call the OnLookupLoaded() method of the STATE control to allow the control to set the undelying value. If we ommit the last call, when editing existing records, the value change script will clear the value of the State control.