Visual Basic Concepts

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:

Suppose that for each row the Calls page 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.

'Open a recordset
rs3.Open "select * from calls", cn1, adOpenStatic, adLockReadOnly

'Write HTML to a generated table
Response.Write "<HTML><BODY><H1>Calls in the System</h1><hr>"
   With Response
    .Write "<table border=1 width=90%>"
    .Write "<tr><th>Call ID</th><th>Call Date</th> _
      <th>Call Time</th><th>Subject</th>"
    Do While rs3.EOF = False
      .Write "<TR><TD>" & rs3("callID") & "</td>"
      .Write "<TD>" & rs3("calldate") & "</td>"
      .Write "<TD>" & rs3("calltime") & "</td>"
      'Add a hyperlink that moves to the CallDetail webitem and calls
      'a dynamically generated event that corresponds to a unique 
      'database key. The key is the CallID value for that row.
      .Write "<TD><a href=""" & URLFor("CallDetail", _
         CStr(rs3("callID"))) & """>" & rs3("subject") & _
         "</a></td></tr>"
    rs3.MoveNext
    Loop
   End With
   Response.Write "</TABLE></HTML>"

To handle the events for the response page, you would define code for this 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)
   'Open a database connection
   objConnection.Provider = "Microsoft.Jet.Oledb.3.51"
   objConnection.Open "c:\final\contact.mdb", "admin", ""
   
   'Retrieve records based on the eventname contained in the 
   'selected row.
   rs3.Open "select * from calls where callID=" & EventName, _
      cn1, adOpenStatic, adLockReadOnly
   CallDetail.WriteTemplate
   cn1.Close
End Sub

In this code, the developer creates a new recordset that contains only the value for the specified key. The CallDetail template, which contains replacement fields into which the recordset's data is inserted, is then written to the browser using the WriteTemplate method.