Referencing Page Controls in Code

The user interface controls defined on the page can be accessed easily within each of the page, table control and record control classes in the code-behind or the controls file. Please note that user interface controls within a table control are initialized and defined when data is loaded into the table control since the number of rows displayed is determined by the result set returned by the query.  As such we recommend that you make most of your code customizations at the Record Control class level since this class is available both when displaying a single record on a page as well as for each row within a table.

Page Class

You can access any user interface control from the page class except the controls that are repeated for each row in a table.  To access the controls from the page, you simply specify the name of the control that is used in your web page.  For example, if you have an Add Customer page, you may have a field label called CompanyNameLabel and a field value text box called CompanyName.  To access this control, you can:

C#:

this.CompanyName

this.CompanyName.Text   // to access the text entered by the user

Visual Basic .NET:

Me.CompanyName

Me.CompanyName.Text ' to access the text entered by the user

If you are displaying an Add, Edit or Show Record page, you will have a record control on the page.  The record control corresponds to the record control class in the page’s code-behind file.  If the page is a Table Report page, you will have a table control on the page and a record control for each row in the table.  Both the table control and the record control will have corresponding classes in the page’s code-behind file.  You can access the record and table controls within the page as follows:

C#:

this.CustomersRecordControl          // to access the record control

// to access the company name within the

// record control

this.CustomersRecordControl.CompanyName

this.CustomersTableControl             // to access the table control

Visual Basic .NET:

Me.CustomersRecordControl ' to access the record control

' to access the company name within the

' record control

Me.CustomersRecordControl.CompanyName

Me.CustomersTableControl ' to access the table control

Table Control Class

The table control class corresponds to the table control on the page.  You can directly access all of the controls within the table except the rows of a table.  To access the rows of a table, you can either override or handle events at the record control class level, or use functions such as GetRecordControls or GetSelectedRecordControls to get an array list that can loop through to get an individual row.  To access the first (or only) selected row, use GetSelectedRecordControl function.  The search, filter, pagination and column sorting controls can be accessed directly from the table control class.

The following example is for a button placed on the button bar of a table control that sets the Discontinued flag on all selected rows of the table control.  The function calls GetSelectedRecordControls to retrieve the list of all selected rows.  For each of the rows (TableControlRow objects), we use the RecordUniqueId to retrieve the database record.  The database record is retrieved using the data access layer function GetRecord on the data access layer Table class (not the TableControl user interface class).  The second argument passed to GetRecord is True to retrieve an updateable record.  The Discontinued flag is modified and the Save function is called to save the record in the database.  The entire For loop is enclosed in a Start, Commit and End Transaction block to ensure that all of the records are committed at the same time.  Finally, the DataChanged variable is set to True so that the PreRender method on the table control refreshes the page with the latest settings from the database.

C#:

public override void MarkDiscontinued_Click(object sender, ImageClickEventArgs args)

{

     ProductsTableControlRow rc;

 

     try {

          DbUtils.StartTransaction();

 

          // Get all of the selected record controls.

          // Record controls are UI controls.

          foreach (rc in this.GetSelectedRecordControls()) {

              ProductsRecord rec;

 

              // Using the RecordUniqueId, read the Product record

              // from the database.

              // Use True as the second argument to make sure you

              // get a Writable record.

              rec = ProductsTable.GetRecord(rc.RecordUniqueId, true);

              if (rec != null) {

                   // Mark as Discontinued

                   rec.Discontinued = true;

                   rec.Save();

              }

          }

 

          DbUtils.CommitTransaction();

     }

     catch (Exception ex) {

 

          Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message);

     }

     finally {

          DbUtils.EndTransaction();

     }

 

     // Mark the data as changed so the data is refreshed

     this.DataChanged = true;

}

Visual Basic .NET:

Public Overrides Sub MarkDiscontinued_Click(ByVal sender As Object, ByVal args As ImageClickEventArgs)

     Dim rc As ProductsTableControlRow

 

     Try

          DbUtils.StartTransaction()

 

          ' Get all of the selected record controls. 

          ' Record controls are UI controls.

          For Each rc In Me.GetSelectedRecordControls()

              Dim rec As ProductsRecord

 

              ' Using the RecordUniqueId, read the Product record

              ' from the database.

              ' Use True as the second argument to make sure you

              ' get a Writable record.

              rec = ProductsTable.GetRecord(rc.RecordUniqueId, True)

              If Not (IsNothing(rec)) Then

                   ' Mark as Discontinued

                   rec.Discontinued = True

                   rec.Save()

              End If

          Next

 

          DbUtils.CommitTransaction()

 

     Catch ex As Exception

          Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", ex.Message)

 

     Finally

          DbUtils.EndTransaction()

     End Try

 

     ' Mark the data as changed so the data is refreshed

     Me.DataChanged = True

End Sub

To retrieve all the currently displayed rows instead of only the selected rows, use the GetRecordControls function.  To retrieve the first (or only) selected row, use the GetSelectedRecordControl function.

All controls within the row can be accessed directly using the name of the control.  In the example above, if the row contained a ProductName control, it can accessed directly by using rc.ProductName.  If the control is a textbox, all properties of the textbox can be accessed directly as well, such as rc.ProductName.Text.

Record Control Class

We recommend most of the code customizations to be made at the record control class.  The record control class corresponds to the record being displayed on the page.  If there is a table displayed on the page, the record control class corresponds to each row within a table.  The customizations will be the same regardless of whether the record control class corresponds to a single record control or to a row within a table.

You can access all of the fields within a record control including column values as shown below:

C#:

this.CompanyName

this.CompanyName.Text   // to access the text entered by the user

Visual Basic .NET:

Me.CompanyName

Me.CompanyName.Text ' to access the text entered by the user