How to create form controls at runtime and dynamically bind them to data.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on data access basics in the Programmer's Guide |
The example shows how you can dynamically create form fields and bind them to a data set through a data source object. When the form is activated, it displays those fields and the appropriate data from the data set.
The following code creates an AgcData object and specifies the data source object (DSO) that it references. The referenced DSO contains data that is independent of the rest of the form.
// Code fragment in formActivate() // Create an AgcData, referencing the Data Source Object, dsoEmployee agcDynamic = new AgcData (); agcDynamic.setName ("agcDynamic"); agcDynamic.setDataSource ("dsoEmployee"); this.add(agcDynamic, 0);
AgcData
object should act as a proxy to the dsoEmployee
data source business object.
The code creates a label and a text field on the form, then uses bind to bind the text field to a property of the specified data source object so that the text field dynamically displays the specified data. You specify the names of the control's "set" and "get" methods. These methods allow the application to get an edited field value and change its corresponding data source property value and the reverse.
// Code fragment in formActivate() // Create an Employee ID Label // Create a field and bind it to the employeeid in agcData AgcLabel idLabel = addLabel ("Employee ID", LABEL_LEFT, FIELD_TOP, LABEL_WIDTH, LABEL_HEIGHT); AgcTextField employeeidField = addTextField (TEXT_LEFT, FIELD_TOP, TEXT_WIDTH, TEXT_HEIGHT); agDataMgr.bind (employeeidField, "Text", "getText", "setText", agcDynamic, new Integer(0) );
agDataMgr.bind()
binds the "Text" property of the employeeidField control to the first property (as specified by index number 0) of the data source.
The following code shows how to invoke the data source object (or business object) for which this AgcData object is a proxy.
// Code fragment in formActivate() // Invoke the DSO try {agcDynamic.invokeQuery("");
agcDynamic.gotoFirst();
} catch (Exception e) { agDialog.displayError(e); }
invokeQuery()
initializes the datastore with the resulting data source.
gotoFirst()
. In this example, the first record is displayed in all the text fields.