Handling Button Events


A good way to catch the relevant button events is by overriding the OnApplicationEvent method and calling your custom logic.
- Pooja Daga, Technical Support Engineer, Iron Speed, Inc.

May 31, 2006
Iron Speed Designer V3.2

Introduction

It is often useful to call custom logic when a button is clicked in your application:
  • Set the initial value of a field control when an Add button is clicked in a child (details) table.
  • Display an alert message when a Delete button is clicked.
  • Send an email confirmation after adding a record.
  • Handle a custom command.

Procedure

A good way to catch the relevant button events is by overriding the OnApplicationEvent method and calling your custom logic. The following code snippet shows some of the event types that can be handled in the OnApplicationEvent method:

Public Overrides Sub OnApplicationEvent(ByVal args As BaseClasses.ApplicationEventArgs)
    Select Case (args.EventType)
          Case ApplicationEventArgs.EventTypes.UpdateData
          Case ApplicationEventArgs.EventTypes.ResetData
          Case ApplicationEventArgs.EventTypes.ResetFilters
          Case ApplicationEventArgs.EventTypes.FilterData
          Case ApplicationEventArgs.EventTypes.ExportData
          Case ApplicationEventArgs.EventTypes.AddRecord
          Case ApplicationEventArgs.EventTypes.DeleteRecord
          Case ApplicationEventArgs.EventTypes.LogOut
    End Select
    MyBase.OnApplicationEvent(args)
End Sub

Overriding the OnApplicationEvent method in a class depends on the control selected in the Bindings tab of the Page Properties dialog for the button. If the control selected in the “Select a control” list box (in the Bindings tab of the button’s Page Properties dialog) is “Page”, then override the OnApplicationEvent in the Page class of the page containing the button. Similarly if the control selected in the “Select a control” list box is “<Table>RecordRow” then override the OnApplicationEvent in the appropriate RecordControl class. Individual pages may contain multiple TableControl and RecordControl classes, so make sure you override the OnApplicationEvent method in the appropriate class.

Handling OnApplicationEvent for a TableControl class

The example below overrides OnApplicationEvent to pre-create several new rows in a detail table so a user can add multiple items without having to click the “Add” button for each item. Since the control selected in the “Select a control” list box (Bindings tab in the Page Properties dialog for the Add button) is “<Table>TableControl”, the OnApplicationEvent method is overridden in the associated TableControl class.

C#:

public override void OnApplicationEvent(BaseClasses.ApplicationEventArgs args)
    switch(args.EventType)
    {
        case(BaseClasses.ApplicationEventArgs.EventTypes.AddRecord):
            if(this.Records.Count <10)
            {
                for(int j=0;j<=10;j++)
                {
                    this.AddRecord();
                }
            }
            break;
    }
    base.OnApplicationEvent (args);
}

Visual Basic .NET:

Public Overrides Sub OnApplicationEvent(ByVal args As BaseClasses.ApplicationEventArgs)
    Select Case (args.EventType)
      Case BaseClasses.ApplicationEventArgs.EventTypes.AddRecord
        Dim i As Integer
        If (Me.Records.Count < 10) Then
            For i = 0 To 10
                Me.AddRecord()
                Next
        End If
    End Select
    MyBase.OnApplicationEvent(args)
End Sub

Note: Be sure to call the MyBase.OnApplicationEvent function.

Handling OnApplicationEvent for a Page class

In the example below, a message is displayed to the user when a button is clicked. Because the control selected in the “Select a control” list box (Bindings tab of the Page Properties dialog for the button) is “Page”, the OnApplicationEvent method is overridden in the Page class.

C#:

public override void OnApplicationEvent(BaseClasses.ApplicationEventArgs args)
{
    switch(args.EventType)
    {
        case(BaseClasses.ApplicationEventArgs.EventTypes.Custom):
          if(args.CustomEventName=="myCustomCommand")
          {
            BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this,"MY_KEY","my message");
          }
          break;
    }
    base.OnApplicationEvent (args);
}

Visual Basic .NET:

Public Overrides Sub OnApplicationEvent(ByVal args As BaseClasses.ApplicationEventArgs)
    Select Case (args.EventType)
        Case BaseClasses.ApplicationEventArgs.EventTypes.Custom
          If (args.CustomEventName = "myCustomCommand") Then
            BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "MY_KEY", "my message")
          End If
    End Select
    MyBase.OnApplicationEvent(args)
End Sub

Note: Be sure to call the MyBase.OnApplicationEvent function.

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