You can get even more flexibility than either the Respond event of a WebItem
or a custom WebItem event provides: You can program with dynamically defined WebItem
events.
Dynamic WebItem event names are unknown at design time. The names of these
events come from information that your code embeds in the HTML sent to the user’s
browser at runtime. You detect the firing of a custom event in the UserEvent event
procedure of the WebItem. UserEvent receives one string parameter, EventName.
EventName contains, of course, the dynamic name of the custom event. To implement
a dynamic event, follow these steps.
STEP BY STEP
17.4 Implementing a Dynamic Event
-
You can embed a two-argument call to URLFor in the WebClass_Start event procedure,
as in step 3 of the preceding section. Instead of hard-coding an existing event’s
name for the second argument to URLFor, however, you can pass a string for which
you have defined no event as the second argument. This string can be either a
hardcoded literal string or a string variable. This technique will dynamically
vary the name of the event that fires when the user clicks the associated hyperlink.
-
In the WebItem’s UserEvent procedure, you can vary the reaction to the
user’s choice of the hyperlink based on the contents of the EventName parameter.
In Listing 17.12, a Select Case structure in the UserEvent procedure traps for
various dynamically generated event names and reacts differently, according to
the name of the event.
LISTING 17.12
TRAPPING DYNAMICALLY DEFINED EVENTS IN THE USEREVENT PROCEDURE
Private Sub Service_UserEvent(ByVal EventName As String)
Select Case UCase$(Trim$(EventName))
Case "AVERAGE"
strServiceColor = AVERAGECOLOR
strServiceDescription = "Average service."
tmpMyFirst.WriteTemplate
Case "EXCELLENT"
strServiceColor = EXCELLENTCOLOR
strServiceDescription = "Excellent service."
tmpMyFirst.WriteTemplate
Case "FAIR"
strServiceColor = FAIRCOLOR
strServiceDescription = "Fair service."
tmpMyFirst.WriteTemplate
Case "GOOD"
strServiceColor = GOODCOLOR
strServiceDescription = "Good service."
tmpMyFirst.WriteTemplate
Case "POOR"
strServiceColor = POORCOLOR
strServiceDescription = "Poor service."
tmpMyFirst.WriteTemplate
End Select
End Sub
A common example of the power of dynamic events would be to generate an event
whose name is based on the key field of a database record. Code in the UserEvent
procedure would detect this event name representing the key field and use it to
perform a lookup on the data.