Unofficial Content

User events are executed using AJAX, which means that intelligence is applied to avoid unnecessary roundtrips to the server and to reduce the amount of data that travels from one side to the other (from server to client and vice versa).

Some user events have to be executed on the server, but others don't. So, the events that don't need to connect to the database should not be executed on the server side; they should be executed on the client side, and that is, in fact, GeneXus users don't need to worry about whether a user event should be executed on one side or the other to achieve better performance in the final application. GeneXus will decide and generate the best code for all cases.

This article explains in detail what the cases are, and how GeneXus solves each of them.

User Events on Client/Server Side

There are two types of events, depending on where they are executed:

  • Client Events: On the client machine (executed by the browser)
  • Server Events: On the server (executed by the web server)

Definition of Client Events

Any user event which can be executed on the client machine (by javascripts) is executed this way. No information travels to the server, because the entire event code can be solved locally by javascripts running on the web browser.

The final goal is to reduce the total number of roundtrips to the web server during web application execution.

The execution of Client user events will give a faster response, and no other event besides that event will be executed. That is to say, the Start, Refresh, Load events won't be executed.

What do we mean by user events?

Any event defined by GeneXus users, except for the following events: Enter, Start, Refresh, Load.

Isvalid Event, Click, OnClickEvent, Drag, Drop, SetContext, TrackContext are also user events.

Client User Events Samples

1. Events that change the status of a control

Event 'X'
   control.visible = 0  //control can be any control of the web form, text block, table, grid, etc.
EndEvent
Event 'X'
   control.enabled = 0  //control can be any control of the web form, text block, table, grid, etc.
EndEvent

2. Assignment of variables (including Structured Data Type variables)

In some cases, the assignment of variables does not need to go beyond the scope of an event.

Suppose that you have a "Products" grid and want the end user to drag a selected product to a cart image. In that case, when the user selects a line of the grid, an SDT containing product ID and name can be loaded.

This event is executed on the client because the &productsdt variable doesn't need to go beyond the scope of this event.

Event products.Drag(&productsdt)
    &productsdt.productid = ProductId
    &productsdt.productdescription = ProductDescription
EndEvent 

However, if a variable is assigned in a user event “A” and is also referenced inside any other Start, Refresh, Load event, the user event "A" will be executed on the server.

For instance, the following code will execute "Event A" on the server-side because the &v01 variable is used in the "&dummy1 = &v01" assignment:

Event Load
 For &i = 1 to 3
  &dummy1 = &v01
  &v01 = &i
  Load
 EndFor
Endevent
Event 'A'
 &v01 = not &v01
Endevent

3. Code inside If - EndIf and Do While - EndDo which can be solved in the client

4. Msg Command is evaluated in the client.

See the following examples:

a. Consider a login form with &username and &userpassword. Check whether the user has entered any &username

Event &UserName.IsValid
    if &UserName.IsEmpty()
       msg('Enter user name please!')
    endif
EndEvent

b. Consider a web form with a "Products" grid with "Notify Context Change" property set to TRUE. This event shows the selected product item in the status bar, without making a roundtrip to the server, merely by executing it on the client machine.

Event trackcontext(productId,productDescription)
      msg( 'Product Description: ' + productdescription, status)
EndEvent

image1cs

Note that the same could have been achieved using the click event of any variable in the grid:

Event &image.Click
    msg( 'Product Description: ' + productdescription, status)
EndEvent

Definition of Server Events

Server user events are always executed using Ajax.

Any User Event which cannot be executed on the client machine (because at least one part of their code needs to be solved on the server side) is executed on the server.

In this case, when the Server event is executed, the Start, Refresh, Load events are also executed. The order in which those events are executed is the order in which events are triggered in a POST execution. The simplest case is as follows:

Naturally, you have to take into account the event triggering order in a page that loads web components and master pages.

There are different cases of Server Events.

  • Events that include a call, link command or web component creation to any other web object (web transaction, web panel, HTTP procedure or HTTP report).
  • Events that include code that requires database access.
  • Events that include calls to batch procedures or reports.
  • If the event code has any variable assignment that is referenced in any other Event (Start, Refresh, Load), the whole event will be executed in the server. This variable reference could be made in an expression, in a condition, through a property change, used as a parameter, etc.
  • If a web control is associated to an event which has no definition (has no source), it is automatically associated to the "Refresh" event, which is a server event.
  • Events that include the SetLanguage function when runtime translation is used

When executed on the server, the event execution is solved by a POST execution (not a submit execution), as explained here.

Server User Event Sample

Suppose that you have a web form with two grids ("Products" and "Companies"); the latter loads in relation to the former. That means that the "Companies" grid filters the companies which distribute the selected product in the "Products" grid. The product is selected by clicking on an image loaded in each row of the grid.

In this case the &image.click event is triggered in the server because the &product variable is used in a load event.

Event &image.click
    &ProductId = ProductId
EndEvent

Event companies.Load()
  for each where productId = &ProductId
    &CompanyId = companyId
    &CompanyName = companyName
    companies.Load()
  endfor
EndEvent

Notes

  • Some property assignments should be triggered on the client side, although there are cases which are still executed on the server side.
  • The Enter event is not considered to be a user event for compatibility (because in web transactions it should be executed on the server even though it's empty). Nevertheless, a distinction could be made between the Enter event in web transactions and the Enter event in web panels.
  • Take into account that when enabling encryption, any event using the PopUp command, Prompt rule will execute on the server side.

 


Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant