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.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

May 31, 2006
Iron Speed Designer V3.2

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 textbox 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. Then, select "Send custom command" in the Bindings tab of the Page Properties dialog and enter the command name "myCustomCommand".

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 OnApplicationEvent method in the TableControl class. The GetSelectedRecord method is used to get the database record for the row that is currently selected.

For .NET Framework 1.1, add the code in the OrdersTableControl class of ShowOrdersTablePage.aspx.cs, located in:

...\<Application Folder>\Orders\ShowOrdersTablePage.aspx.cs

For .NET Framework 2.0, add the code in the OrdersTableControl class of ShowOrdersTablePage.Controls.cs, located in:

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

C#:

public override void OnApplicationEvent(BaseClasses.ApplicationEventArgs args)
{
    if(args.CustomEventName=="myCustomCommand")
    {
      OrdersRecord mySelectedRecord = this.GetSelectedRecord(false);
      if(mySelectedRecord != null)
      {
        this.Textbox.Text = mySelectedRecord.CustomerID.ToString() + " " +
        mySelectedRecord.EmployeeID.ToString();
      }
      else       {
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "my_Key", "Please select a record");
      }
    }
    base.OnApplicationEvent (args);
}

Visual Basic .NET:

Public Overrides Sub OnApplicationEvent(ByVal args As BaseClasses.ApplicationEventArgs)
    If args.CustomEventName = "myCustomCommand" Then
        Dim mySelectedRecord As OrdersRecord = Me.GetSelectedRecord(False)
        If (Not mySelectedRecord Is Nothing) Then
          Me.Textbox.Text = mySelectedRecord.CustomerID.ToString + " " +
          mySelectedRecord.EmployeeID.ToString
        Else
          BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "my_Key", "Please select a record")
        End If
    End If
    mybase.OnApplicationEvent(args)
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.

About the Author
Pooja Daga
Technical Support Engineer, Iron Speed, Inc.

Pooja has experience developing Web applications using .NET technology. She started her career with PCS, a leading software services company headquartered in India and has been with Iron Speed since 2005.

Pooja holds an M.S. degree in Computer Application and a B.S. degree in Electrical engineering from Maharaja Sayajirao University in India.



  Privacy Statement