Defining Webclass Events at Run Time

See Also

In addition to handling events that exist in the webclass at design time, you can create events at run time and process them dynamically. The ability to generate events at run time is useful in cases where you generate part or all of your user interface dynamically, by writing HTML in your response to the browser.

You generate user events at run time using the URLFor method, which takes two arguments: the name of a webitem in your webclass, and the name of an event. To create a user event, you pass this function the name of an event that was not defined at run time.

Processing for these run-time events is handled by the UserEvent event. In the procedure for this event, you write code that tells the webclass how to process all its dynamically generated events. If there is more than one of these events, you must use a conditional statement such as If or Select Case to tell the webclass how to respond to each of them.

Example: Dynamic Events in a Search Page

Suppose you have an application that consists of two pages:

The following figure shows two possible outcomes of the search.

Tables with Dynamically Generated Events

Suppose that for each row the search returns, you want to generate a hyperlink the user can select to receive more information. These hyperlinks can act as events for the webclass, but you cannot handle these events at design time because they are created dynamically when the search is executed. Therefore, you must generate these events at run time.

The following shows some of the Visual Basic code you would use to generate the table and create user events for it.

With Response

   'Begin a table
   .Write "<TABLE BORDER CELLSPACING=1 CELLPADDING=7>"

   'For each record in the result set, generate a row.
   Do While rs.EOF = False
      'Start a row and cell
      .Write "<TR><TD>"

      'Insert a hyperlink calling the lastname field,
      'which represents a database key into the database
      'record, for the Response webitem
      .Write  "<A HREF=""" & URLFor(Response, rs("lastname") & """>"

      'Make the text of the hyperlink the person's name.
      .Write rs("lastname") & ", " & rs("firstname")
      .Write </A>"
      .Write "</TD></TR>

      'Move to the next record and loop
      rs.MoveNext
   Loop

   'End the table
   .Write "</TABLE>"

End With

To handle the events for the response page, you would define code for the Response webitem's UserEvent event.

Coding User Events

You handle all user events for a webitem within a single UserEvent procedure. If you have a page that contains several user events and you need to treat them differently in your procedures, you would use a conditional statement such as If or Select Case to specify the different responses for each dynamic event name on the page.

Note   In the case of the table generated in the previous section, you would not use an If or Select Case statement because you want to perform the same operation for each row — retrieve the database key from the table, then look up the record and display more information.

The webclass fires the UserEvent event when it receives a request from the browser that references a user event. The following code shows the procedure you might define for the events in the table shown in the section above:

Private Sub Response_UserEvent(ByVal EventName As String)

   'Set a previously-defined variable to a new recordset
   rs = New Ado.recordset

   'Retrieve the key
   rs.GetPerson EventName

   'Code here to display a page with all of the information
   'for the retrieved record.
End Sub

In this code, the developer creates a new recordset and retrieves the value for the specified key. You would replace the word Key with the appropriate key value.