Handling a Selected Row in a Table

It is often useful to call custom application logic when a row is selected in a table control in your application.
- Anh Trinh, Software Engineer, Iron Speed, Inc.

October 11, 2006
Iron Speed Designer V4.0

Introduction
It is often useful to call custom application logic when a row is selected in a table control in your application:
  • Pass the total of two fields in a selected record as a URL parameter.
  • Display totals of multiple fields in a text box.
  • Modify fields in a selected record.
The example below sets text into a text box when a button is clicked within a table row. The specific row is determined and the text is concatenated from two field values in the selected record.
Procedure
Step 1: Using the Application Wizard in Iron Speed Designer, create a sample application using the Orders table in the Northwind database.

Step 2: Drag a button control from the Iron Speed Designer tool box and place it next to the Export button in the Show Orders Table page. Set the Button text to "myButton" in the Display tab of the Page Properties dialog for the button. Finally, in the Bindings tab of the Page Properties dialog, select "Custom" and "Enable client-side validation".

Step 3: Drag a text box control from the Iron Speed Designer tool box and place it on the page. Select an appropriate database table and database field in the Bindings tab of the Page Properties dialog box of the text box control. This binds the control to the database so that it can fetch and update data from the database

Step 4: Override the Button_Click() method in the TableControl class. Use the GetSelectedRecord() method to get the database record for the row that is currently selected.

For .NET Framework 1.1, add your code in the OrdersTableControl class, located in:

...\<Application Folder>\Orders\ShowOrdersTablePage.Controls.cs or .vb

For .NET Framework 2.0, add your code in the OrdersTableControl class, located in:

...\<Application Folder>\App_Code\Orders\ShowOrdersTablePage.Controls.cs or .vb

C#:

public override void Button_Click(object sender, EventArgs args)
{
    try
    {
        DbUtils.StartTransaction();
        OrdersRecord mySelectedRecord = this.GetSelectedRecordControl().GetRecord();
        if(mySelectedRecord != null)
        {
                this.Textbox.Text = mySelectedRecord.CustomerID.ToString() + " " +
                mySelectedRecord.EmployeeID.ToString();
        }
    }
    catch(Exception ex)
    {
        DbUtils.RollBackTransaction();
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "my_Key", "Please select a record");
    }
    finally
    {
        DbUtils.EndTransaction();
    }
}

Visual Basic .NET:

Public Overrides Sub Button_Click(ByVal sender As Object, ByVal args As EventArgs)
    Try
        DbUtils.StartTransaction
        Dim mySelectedRecord As OrdersRecord = Me.GetSelectedRecordControl.GetRecord
        If (Not (mySelectedRecord) Is Nothing) Then
          Me.Textbox.Text = (mySelectedRecord.CustomerID.ToString + (" " +
          mySelectedRecord.EmployeeID.ToString))
        End If
    Catch ex As Exception
        DbUtils.RollBackTransaction
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "my_Key", "Please select a record")
    Finally
        DbUtils.EndTransaction
    End Try
End Sub

Note: "Textbox" is the name of the text box control in the HTML layout page file.

Step 5: Build and run your application.

Note: You can also use the "Handle Selected Record" code customization example in the "Selected Row Handling" section of the Code Customization Wizard in Iron Speed Designer. Note: even though you may select multiple records, only the first record is used.

About the Author
Anh Trinh
Software Engineer, Iron Speed, Inc.

Anh is a software engineer at Iron Speed, Inc. He enjoys developing applications with Iron Speed Designer and Microsoft .NET technology.



  Privacy Statement