Application Techniques



Dynamically Binding Controls to Data

How to create form controls at runtime and dynamically bind them to data.

About this technique

Details

Category

Data Access Techniques> General

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

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.

Creating a control that accesses an independent data set   Top of page

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); 

Notes about the code

Binding the property of a text field to a data source property   Top of page

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) ); 

Notes about the code

Invoking the data source object   Top of page

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); 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.