Handling events

DataWindow .NET follows the delegate event-handling model used in the .NET Framework.

Senders and delegates

An event can be triggered when a user clicks a button or moves a mouse, or when your code triggers the event explicitly. The object that triggers the event is called the event sender. The event sender has no knowledge of the object that captures the event (the event receiver), and the role of the delegate is to act as an intermediary between the sender and receiver.

The delegate is a special class that takes information about the sender of the event and the data associated with the event and communicates it to the method that actually handles the event.

An “On” method triggers an event

An event is triggered by a protected method that has the name of the event with the prefix On. For example, the RowFocusChanging event for the DataWindowControl is triggered by the OnRowFocusChanging method.

The method that triggers the event has one argument that inherits from the System.EventArgs class and contains data associated with the event. The argument for OnRowFocusChanging is RowFocusChangingEventArgs.

Delegate naming and arguments

The delegate for an event has the same name as the event with the suffix EventHandler. For example, the delegate for RowFocusChanging is RowFocusChangingEventHandler. A delegate has two arguments: a reference to the System.Object that triggered the event (the sender) and the descendant of the System.EventArgs class specifically associated with the event. The second argument can also be a specialized descendant of System.EventArgs called CancelEventArgs—this argument can be used when the event can be cancelled.

EventArgs argument

The descendant of EventArgs associated with an event has properties specific to the event. For example, RowFocusChangingEventArgs has three properties:

For an example of coding an event handler, see “Retrieving data”.

Event scripts for user-defined buttons

In DataWindow Designer, you can add buttons to a DataWindow with either a predefined action, such as Update or Retrieve, or a user-defined action. If you use a predefined action, the code to perform the action is provided for you. If you select User-Defined (the default) from the Action list in the DataWindow painter, you need to code a ButtonClicked event for the button.

Suppose you add two buttons named b_button1 and b_button2 to a DataWindow object in the DataWindow painter. You can add a ButtonClicked event to the DataWindow control in .NET to perform a different action depending on which button was clicked:

Private Sub dwGrid_ButtonClicked(ByVal sender As Object, ByVal e As Sybase.DataWindow.ButtonClickedEventArgs) Handles dwGrid.ButtonClicked

   Dim buttonClicked As String
   buttonClicked =   _
      dwGrid.ObjectUnderMouse.Gob.Name.ToString()
   If buttonClicked = "b_button1" Then
      MsgBox("Button 1 was Clicked!")
   ElseIf buttonClicked = "b_button2" Then
      MsgBox("Button 2 was clicked!")
   End If
End Sub

Events in Web applications

Events that take place in the client browser in an ASP.NET Web application can be handled in client events. Client-side methods or built-in buttons that cause a page round trip trigger the BeforePerformAction and AfterPerformAction server-side events. For more information, see “About client-side programming”.